You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes, a generic will construct a type which keys allow undefined but are not marked as optional. The proposed NormalizeOptional<Type> will mark such keys as optional.
typeDefineParams<Keysextendsstring,Defaults>={[KinKeys]: KextendskeyofDefaults ? number|undefined : number}constdefaults={foo: 1}typeParams=DefineParams<"foo"|"bar",typeofdefaults>functionf1(params: Params){}// Error: Property 'foo' is missing in type '{ bar: number; }' but required in type 'Params'f1({bar: 1})// OK, but has redundant undefinedf1({foo: undefined,bar: 1})functionf2(params: NormalizeOptional<Params>){}// OKf2({bar: 1})
Additional Info
No response
The text was updated successfully, but these errors were encountered:
By the way, I just happened to face a reverse problem (vueuse/vueuse#3546): I needed to unmark keys accepting undefined as being optional:
typeA={foo: string;bar?: string}typeFixedA={foo: string;bar: string|undefined}// bar always exists in keys (even if the value is undefined)
I see.
One thing to add here is that composition of MarkOptional and UnmarkOptional isn't going to be homomorphic in general.
This would be complicated to find out about so developers need to be very careful with such feature request. It may bring a lot of confusion so picking right name would be very challenging here
Hey @IlyaSemenov! As some time passed, do you still use this utility type, how does it behave in the codebase, did you find out about the edge cases and best practices?
What
I propose type
NormalizeOptional<Type>
:With implementation similar to:
Examples
Sometimes, a generic will construct a type which keys allow undefined but are not marked as optional. The proposed
NormalizeOptional<Type>
will mark such keys as optional.Additional Info
No response
The text was updated successfully, but these errors were encountered: