这是某些功能lab
的实际(简化)示例
const lab = <
T extends Something,
K extends keyof T
>(k: T[K] extends (...args: any[]) => any ? K : never) => {
return ({} as T)[k];
}
const test = lab<Something, 'fn'>('fn')
type Something = {
str: string,
num: number
bool: boolean,
fn: (a: number, b: number) => number,
}
它的工作效果如预期的那样.但通过下面的屏幕,我展示了我也想要的东西,但不明白为什么这是不可能的.
下面的一些屏幕肯定是一般类型是有效的.
allow str
in generic placeholder as key of Something
but error with argument ... so aka works
event if argument become str
also error ... so aka works
allow fn
and tip it in argument ... good!
yes, this is only one correct variant and no errors! works