假设我有这个:
type KeyForValueType<T, ValueType> = { [K in keyof T]: T[K] extends ValueType ? K : never; }[keyof T];
declare function fn1<T, ValueType>(obj: T, key: KeyForValueType<T, ValueType>): void;
fn1
will work as expected, it will properly limit the key I can use based on the type of value of that key:
fn1<{ a: number; b: string }, string>({ a: 11, b: 'str' }, 'a'); // error as expected
fn1<{ a: number; b: string }, string>({ a: 11, b: 'str' }, 'b'); // works
fn1<{ a: number; b: string }, number>({ a: 11, b: 'str' }, 'a'); // works
fn1<{ a: number; b: string }, number>({ a: 11, b: 'str' }, 'b'); // error as expected
I got the above from here: https://stackoverflow.com/a/51476941/520229
但是,当实际实现该函数时,我无法使它识别值的类型:
// with generic ValueType
function fn2<T, ValueType>(obj: T, key: KeyForValueType<T, ValueType>): void {
const value: ValueType = obj[key];
console.log(value);
}
// or even fixed value type
function fn3<T>(obj: T, key: KeyForValueType<T, string>): void {
const value: string = obj[key];
console.log(value);
}
Check it out here.
我在这里做错什么了吗?这甚至可能与打字稿有关?