相关疑难解决方法(0)

TypeScript:选择具有定义类型的属性

是否可以构造一个 TypeScript 类型,它只会选择那些 typeof X 的属性?

interface IA {
    a: string;
    b: number;
}

interface IB extends IA {
    c: number | string;
}

type IAStrings = PickByType<IA, string>;
// IAStrings = { a: string; }
type IBStrings = PickByType<IB, string>;
// IBStrings = { a: string; }
type IBStringsAndNumbers = PickByType<IB, string | number>;
// IBStringsAndNumbers = { a: string; b: number; c: number | string; }
Run Code Online (Sandbox Code Playgroud)

typescript

7
推荐指数
2
解决办法
3235
查看次数

标签 统计

typescript ×1