【发布时间】:2021-07-26 08:54:27
【问题描述】:
假设我有一个在代码库中使用的现有 typescript 接口
interface example<condition extends true | false = false> {
a: string;
b: string;
c: condition extends true ? string : number;
// many other properties
}
现在如果条件为真,我想保留属性 a,但如果条件为假,则将属性 a 替换为属性 d,怎么能我做吗?
是否有任何简单的方法,例如 javascript 对象如何使用它,如下所示(虽然有语法错误)
interface example<condition extends true | false = false> {
...(condition extends true && {a: string});
...(condition extends false && {d: string});
b: string;
c: condition extends true ? string : number;
// many other properties
}
【问题讨论】:
标签: typescript