@@ -383,12 +383,13 @@ export function parseSync<T>(
383383) : Result < T > {
384384 let initialState = parser . initialState ;
385385
386- // Inject annotations into state if provided
387- if ( options ?. annotations ) {
386+ // Inject annotations into state if provided, but skip for null/undefined
387+ // initial states (e.g., undefined for withDefault/optional) to preserve
388+ // their semantic meaning. Corrupting undefined to { [annotationKey]: ... }
389+ // breaks parsers that rely on typeof state === "undefined" checks.
390+ if ( options ?. annotations && initialState != null ) {
388391 initialState = {
389- ...( typeof initialState === "object" && initialState !== null
390- ? initialState
391- : { } ) ,
392+ ...( typeof initialState === "object" ? initialState : { } ) ,
392393 [ annotationKey ] : options . annotations ,
393394 } as unknown ;
394395 }
@@ -452,12 +453,13 @@ export async function parseAsync<T>(
452453) : Promise < Result < T > > {
453454 let initialState = parser . initialState ;
454455
455- // Inject annotations into state if provided
456- if ( options ?. annotations ) {
456+ // Inject annotations into state if provided, but skip for null/undefined
457+ // initial states (e.g., undefined for withDefault/optional) to preserve
458+ // their semantic meaning. Corrupting undefined to { [annotationKey]: ... }
459+ // breaks parsers that rely on typeof state === "undefined" checks.
460+ if ( options ?. annotations && initialState != null ) {
457461 initialState = {
458- ...( typeof initialState === "object" && initialState !== null
459- ? initialState
460- : { } ) ,
462+ ...( typeof initialState === "object" ? initialState : { } ) ,
461463 [ annotationKey ] : options . annotations ,
462464 } as unknown ;
463465 }
@@ -578,12 +580,13 @@ export function suggestSync<T>(
578580
579581 let initialState = parser . initialState ;
580582
581- // Inject annotations into state if provided
582- if ( options ?. annotations ) {
583+ // Inject annotations into state if provided, but skip for null/undefined
584+ // initial states (e.g., undefined for withDefault/optional) to preserve
585+ // their semantic meaning. Corrupting undefined to { [annotationKey]: ... }
586+ // breaks parsers that rely on typeof state === "undefined" checks.
587+ if ( options ?. annotations && initialState != null ) {
583588 initialState = {
584- ...( typeof initialState === "object" && initialState !== null
585- ? initialState
586- : { } ) ,
589+ ...( typeof initialState === "object" ? initialState : { } ) ,
587590 [ annotationKey ] : options . annotations ,
588591 } as unknown ;
589592 }
@@ -650,12 +653,13 @@ export async function suggestAsync<T>(
650653
651654 let initialState = parser . initialState ;
652655
653- // Inject annotations into state if provided
654- if ( options ?. annotations ) {
656+ // Inject annotations into state if provided, but skip for null/undefined
657+ // initial states (e.g., undefined for withDefault/optional) to preserve
658+ // their semantic meaning. Corrupting undefined to { [annotationKey]: ... }
659+ // breaks parsers that rely on typeof state === "undefined" checks.
660+ if ( options ?. annotations && initialState != null ) {
655661 initialState = {
656- ...( typeof initialState === "object" && initialState !== null
657- ? initialState
658- : { } ) ,
662+ ...( typeof initialState === "object" ? initialState : { } ) ,
659663 [ annotationKey ] : options . annotations ,
660664 } as unknown ;
661665 }
@@ -916,12 +920,13 @@ function getDocPageSyncImpl(
916920) : DocPage | undefined {
917921 let initialState = parser . initialState ;
918922
919- // Inject annotations into state if provided
920- if ( options ?. annotations ) {
923+ // Inject annotations into state if provided, but skip for null/undefined
924+ // initial states (e.g., undefined for withDefault/optional) to preserve
925+ // their semantic meaning. Corrupting undefined to { [annotationKey]: ... }
926+ // breaks parsers that rely on typeof state === "undefined" checks.
927+ if ( options ?. annotations && initialState != null ) {
921928 initialState = {
922- ...( typeof initialState === "object" && initialState !== null
923- ? initialState
924- : { } ) ,
929+ ...( typeof initialState === "object" ? initialState : { } ) ,
925930 [ annotationKey ] : options . annotations ,
926931 } as unknown ;
927932 }
@@ -950,12 +955,13 @@ async function getDocPageAsyncImpl(
950955) : Promise < DocPage | undefined > {
951956 let initialState = parser . initialState ;
952957
953- // Inject annotations into state if provided
954- if ( options ?. annotations ) {
958+ // Inject annotations into state if provided, but skip for null/undefined
959+ // initial states (e.g., undefined for withDefault/optional) to preserve
960+ // their semantic meaning. Corrupting undefined to { [annotationKey]: ... }
961+ // breaks parsers that rely on typeof state === "undefined" checks.
962+ if ( options ?. annotations && initialState != null ) {
955963 initialState = {
956- ...( typeof initialState === "object" && initialState !== null
957- ? initialState
958- : { } ) ,
964+ ...( typeof initialState === "object" ? initialState : { } ) ,
959965 [ annotationKey ] : options . annotations ,
960966 } as unknown ;
961967 }
0 commit comments