- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.1k
          fix: typescript requires fields when draft: true despite passing draft: true
          #14271
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| 📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖 
 Largest pathsThese visualization shows top 20 largest paths in the bundle.Meta file: packages/next/meta_index.json, Out file: esbuild/index.js
 Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js
 Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js
 Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
 Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js
 Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js
 DetailsNext to the size is how much the size has increased or decreased compared with the base branch of this PR. 
 | 
| 🚀 This is included in version v3.61.0 | 
| Hello :) I've have a use case where I want to be able to query some draft documents via the Local API but obviously in this case the required fields are still possibly null. The returned types with  I've updated to v3.61.0 to see if this PR fixes it however when querying this type mismatch is still there. This covers the case for  | 
| @PatrikKozak @jessrynkar I've opened up a draft PR for the above to fix the types here - checked the typings locally and this works as expected with this change now -> #14388 | 
What?
Fixes TypeScript types for
payload.create()to allow omitting required fields whendraft: trueis specified, aligning type checking with runtime validation behavior.Why?
According to the documentation, when creating drafts, required fields don't need to be provided because validation is skipped by default (unless
versions.drafts.validateis explicitly set totrue). However, TypeScript was still enforcing required fields at the type level, causing type errors even whendraft: truewas set.How?
DraftDataFromCollectionSlugandDraftDataFromCollectionthat make all fields optionalpayload.createoptions to use a discriminated union type:draft: true→ usesDraftDataFromCollectionSlug(all fields optional)draft: falseor omitted → usesRequiredDataFromCollectionSlug(required fields enforced)The fix uses TypeScript's discriminated unions to provide accurate type inference based on the
draftparameter value.Fixes #12578