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
interfaceTest{optional?: string;}functiongetProperty<KextendskeyofTest>(t: Test,key: K): Test[K]|never{if(t[key]){returnt[key];}thrownewError('Value does not exist at key');}consta: string=getProperty({},'optional');
Target behavior:
The given function is coded such that it will always return a non-empty value, so it would be great if there were a way to make something like this compile. Is there a way to communicate the opposite of Partial<T>, e.g. that we knowTest[K] will be non-empty?
Actual behavior: error TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
You can use inference from homomorphic mapped types in #12528 to get a type that removes the undefined
for instance:
interfaceTest{optional?: string;required: number;}typeOptionalMap<T>={[PinkeyofT]: T[P]|undefined;};functiongetProperty<T,KextendskeyofT>(t: T,key: K): T[K]{returnt[key];}functiongetPropertyRemoveUndefined<T,KextendskeyofT>(t: OptionalMap<T>,key: K): T[K]{// Put it in a value, since `t[p]` is not narrowed only t.p which is not possible herevarvalue=t[key];if(value){returnvalue;}thrownewError('Value does not exist at key');}vart: Test={required: 0};varo1=getProperty(t,'optional');// string | undefinedvarr1=getProperty(t,'required');// numbervaro2=getPropertyRemoveUndefined(t,'optional');// stringvarr2=getPropertyRemoveUndefined(t,'required');// number
Reacted by opensrcken, Guntars Ašmanis-Graham and Kay Gosho
I just merged #12589 and now you can simply use the predefined Partial<T> type:
interfaceTest{optional?: string;required: number;}functiongetProperty<T,KextendskeyofT>(t: T,key: K): T[K]{returnt[key];}functiongetPropertyRemoveUndefined<T,KextendskeyofT>(t: Partial<T>,key: K): T[K]{// Put it in a value, since `t[p]` is not narrowed only t.p which is not possible herevarvalue=t[key];if(value){returnvalue;}thrownewError('Value does not exist at key');}vart: Test={required: 0};varo1=getProperty(t,'optional');// string | undefinedvarr1=getProperty(t,'required');// numbervaro2=getPropertyRemoveUndefined(t,'optional');// stringvarr2=getPropertyRemoveUndefined(t,'required');// number
Reacted by opensrcken, Aluan Haddad, Frederico Galvão and SlurpTheo
opensrcken BTW, I noticed that you used a union Test[K] | never. Just wanted to point out that the never type is redundant here as it is always removed from union types (it effectively represents an empty union type).
Reacted by opensrcken, Aluan Haddad, SlurpTheo and HermannGruber
So I must write a function to get these opposite type ?
Can I just remove undefined or null from a type just like use ! ?
Reacted by kraftwer1, Robert R., Alvis Tang, Zhongyang Wang, Paul Li, Andrei Senaev, Noel Abrahams, Chris Dell, Kevin Saldaña, Tom Crockett and 15 more
Activity
mhegazy commentedon Nov 30, 2016
You can use inference from homomorphic mapped types in #12528 to get a type that removes the
undefinedfor instance:
ahejlsberg commentedon Nov 30, 2016
I just merged #12589 and now you can simply use the predefined
Partial<T>type:ahejlsberg commentedon Nov 30, 2016
opensrcken BTW, I noticed that you used a union
Test[K] | never. Just wanted to point out that thenevertype is redundant here as it is always removed from union types (it effectively represents an empty union type).opensrcken commentedon Nov 30, 2016
It works. And point taken on
neverin unions. Thanks guys. 👍k8w commentedon Jun 23, 2017
So I must write a function to get these opposite type ?
Can I just remove undefined or null from a type just like use
!?mjomble commentedon Jun 1, 2018
TypeScript 2.8 introduces
Required<T>that may give you what you need: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#example-6