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
In situations in which I wish to use a partial get function TypeScript doesn't seem able to work out the return type correctly.
TypeScript 2.5.3
interfaceProps{property: boolean;someProperty: string;someOtherProperty: number;}constobj={property: true,someProperty: 'some property',someOtherProperty: 9999}functionget1<T,KextendskeyofT=keyofT>(k: K){return(obj: T): T[K]=>obj[k]}functionget2<T,KextendskeyofT=keyofT>(k: K,obj: T): T[K]{returnobj[k]}// Why do I need to supply 'someProperty' as the second type argument to get this to work?constgetSomeProperty=get1<Props>('someProperty')constsomeProperty=getSomeProperty(obj)// Type is `string | number | boolean`// The type is worked out correctly here.constsomeProperty2=get2('someProperty',obj);// Type is `string`
Other than calling get1<Props, 'someProperty'>('someProperty') what could I do to dynamically create a getSomeProperty function which returns a string?
// Perhaps it was wrong for me to set a default type for K, but if I remove it completely then// on usage of `get1` I get an error message: "Expected 2 type arguments, but got 1" which// as far as I can tell, still has the same problem and forces the same inelegant approach.functionget1<T,KextendskeyofT>(k: K){return(obj: T): T[K]=>obj[k]}
Sorry, if this isn't the right place for this. I use this style of coding quite frequently in libraries that I write, and I want to find out whether this feature request is being tracked somewhere so I can follow it. Or if there is already some elegant syntax to do this, I want to know what it is!
The text was updated successfully, but these errors were encountered:
Closed as this was the wrong repository... See: microsoft/TypeScript#14400 (comment)
In situations in which I wish to use a partial
get
function TypeScript doesn't seem able to work out the return type correctly.TypeScript 2.5.3
Other than calling
get1<Props, 'someProperty'>('someProperty')
what could I do to dynamically create agetSomeProperty
function which returns astring
?Sorry, if this isn't the right place for this. I use this style of coding quite frequently in libraries that I write, and I want to find out whether this feature request is being tracked somewhere so I can follow it. Or if there is already some elegant syntax to do this, I want to know what it is!
The text was updated successfully, but these errors were encountered: