请教一道 TypeScript 的面试题
資深大佬 : fdd 1
实现一个 ts 的工具函数 GetOnlyFnProps<T> ,提取泛型类型 T 中字段类型是函数的工具函数,其中 T 属于一个对象。
// 答案 type GetOnlyFnKeys<T extends object> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T] // 不太理解这个地方 type GetOnlyFnProps<T extends object> = { [K in GetOnlyFnKeys<T>]: T[K] } // 测试用例 // 除了函数类型,其他都删除了 type obj = { a: () => string, b: number } type ccc = GetOnlyFnProps<obj> let value:ccc = { a: () => '1' }
大佬有話說 (2)