MST

星途 面试题库

面试题:TypeScript Pick工具类型与条件类型的结合运用

给定一个通用接口interface GenericObject<T> { [key: string]: T; },以及一个条件类型interface Condition<T> { isString: T extends string ? true : false; }。请使用Pick工具类型和Condition条件类型,实现一个新类型,该类型从GenericObject中挑选出值为字符串类型的键值对。
30.9万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
interface GenericObject<T> { 
    [key: string]: T; 
}
interface Condition<T> { 
    isString: T extends string ? true : false; 
}
type PickStringKeys<T extends GenericObject<any>> = {
    [K in keyof T as Condition<T[K]>['isString'] extends true ? K : never]: T[K];
};