I am trying to setup a generic where the parameter is the key of another object. I can accomplish this using extends keyof
when both types are parameters are to the function.
However when the type providing the list of keys is not an argument, just a generic type, typescript requires both generic types to be set.
Consider the following code:
interface Bar {
z: string;
t: number;
}
declare function foo1<T extends keyof Bar>(x: T)
let t1 = foo1('z');
declare function foo2<K, T extends keyof K>(x: T)
let t2 = foo2<Bar>('t');
declare function foo3<T>(x: T)
let t3 = foo3<keyof Bar>('t');
Function foo2
fails since the second type T
is not specified. However I feel like TS should be able to infer the correct typings without an explicit second type.
foo3
is my workaround, but isn't as nice to work with, is it possible to have typescript perform this inference, or will this be a feature request / bug report to the TS team?
You are close with foo2
. Here is what it's supposed to be to make it work.
declare function foo2<T, K = keyof T>(x: K)
let t2 = foo2<Bar>('t');
Instead of expecting a new generic type parameter, you can assign one to a type (in this case the type of T
).
It's also possible to directly assign the paramaters type to the keyof
the given generic type T
.
declare function foo4<T>(x: keyof T);
let t4 = foo4<Bar>('t');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments