TS第二个参数类型是根据第一个参数类型
interface person{ name: string id: number sex: boolean }
func1<T extends keyof person,K extends person>(
a: T, b: K[T]
){
}
简化一下
func1<T extends keyof person>(
a: T, b: person[T]
){
}
调用方法
changeName(){ this.func1("name", "2") this.func1("name", 2) // 报错 this.func1("id", "2") // 报错 this.func1("id", 2) this.func1("sex", true) }
漫思