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
{{ message }}
Repository navigation
'Omit' should alias a distinct mapped type (for display purposes) #31104
Today, Omit will expand to Pick<Exclude<...>, ...> in quick info, error messages, etc. which is gross.
By defining Omit as its own conditional type, we can get a slightly nicer display, though it will introduce duplication of code between the two helpers.
By defining Omit as its own conditional type, we can get a slightly nicer display, though it will introduce duplication of code between the two helpers.
Actually it's the mapped type - you'd want to inline the Pick, not the Exclude. So:
changed the title [-]'Omit' should alias its own conditional for display[/-][+]'Omit' should alias a distinct mapped type (for display purposes)[/+]on Apr 25, 2019
// default type parameter trickerytypeOmit<T,Kextendskeyofany,XextendskeyofT=Exclude<keyofT,K>>={[PinX]: T[X]};interfaceTest{required: string;optional?: string;readonlyviewonly: string;}declareconstx: Omit<Test,"required"|"optional"|"notPresent">;x.viewonly="okay";// error!// Cannot assign to 'viewonly' because it is a read-only property.// typeof x is Omit<Test, "required" | "optional" | "notPresent", "viewonly">>
Pro: The type parameter X maintains the homomorphicitinessitude that you need in the mapped type.
Pro: The name Omit will appear instead of Pick and Exclude in IntelliSense
Con: Default type parameters are not meant for this, who knows what could happen
Con: IntelliSense will show the value of X which could be confusing.
Or what about
// conditional type inference anticstypeOmit<T,Kextendskeyofany>=Pick<T,Exclude<keyofT,K>>extends infer U
? {[PinkeyofU]: U[P]}
: never;interfaceTest{required: string;optional?: string;readonlyviewonly: string;}declareconstx: Omit<Test,"required"|"optional"|"notPresent">;x.viewonly="okay";// error!// Cannot assign to 'viewonly' because it is a read-only property.// typeof x is { readonly viewonly: string; }
Pro: Still homomomomomorphic
Pro: The name Omit appears instead of Pick, and in the concrete cases the actual distinct mapped type shows up.
Con: Conditional type inference isn't exactly meant for this either, who knows what can happen
Also, Wesley Wigham (@weswigham) may want to weigh in since he has a PR out at #37608, but I think it would be undesirable to add a 3rd type parameter just to use a default type argument.
Activity
weswigham commentedon Apr 24, 2019
Actually it's the mapped type - you'd want to inline the
Pick, not theExclude. So:❤️
DanielRosenwasser commentedon Apr 25, 2019
Yeah, I figured it out as I made the change.
[-]'Omit' should alias its own conditional for display[/-][+]'Omit' should alias a distinct mapped type (for display purposes)[/+]DanielRosenwasser commentedon May 1, 2019
Looks like this is getting reverted because of #31190.
jcalz commentedon May 3, 2019
Maaaaybe something like this?
Pro: The type parameter
Xmaintains the homomorphicitinessitude that you need in the mapped type.Pro: The name
Omitwill appear instead ofPickandExcludein IntelliSenseCon: Default type parameters are not meant for this, who knows what could happen
Con: IntelliSense will show the value of
Xwhich could be confusing.Or what about
Pro: Still homomomomomorphic
Pro: The name
Omitappears instead ofPick, and in the concrete cases the actual distinct mapped type shows up.Con: Conditional type inference isn't exactly meant for this either, who knows what can happen
DanielRosenwasser commentedon Sep 21, 2020
The following type
now maintains modifiers thanks to Anders Hejlsberg (@ahejlsberg)'s pull request at #40633.
DanielRosenwasser commentedon Sep 21, 2020
I think this change will be contingent on performance tests though.
DanielRosenwasser commentedon Sep 21, 2020
Also, Wesley Wigham (@weswigham) may want to weigh in since he has a PR out at #37608, but I think it would be undesirable to add a 3rd type parameter just to use a default type argument.
ExE-Boss commentedon Sep 22, 2020
For TS 4.0 and older, you can use Jack Works (@Jack-Works)’s type-challenges/type-challenges#141.