请考虑以下代码.
type Foo<K extends string, T extends Record<K, string>> = {
keys: K
value: T
}
function bar<T extends Foo<'1' | '2', { 1: 'a', 2: 'b' }>>(def: T['value'], key: T['keys']) {
// Type 'T["keys"]' cannot be used to index type 'T["value"]'.(2536)
return def[key];
}
T['value']
确实被T['key']
编入索引,但TS抱怨说这是不允许的.
为什么会发生这种奇怪的行为?你能提供一个正确的片段吗?