【发布时间】:2018-07-06 04:55:04
【问题描述】:
我很确定 Typescript 能够根据属性值确定扩展类,例如:
interface Base {
type: string;
child?: Base;
}
interface Ext extends Base {
type: 'test';
sth: string;
}
z({
type: 'a',
child: {
type: 'b',
}
}); // ok
z({
type: 'a',
child: {
type: 'test',
sth: 'val'
}
}); // not ok
function z(input: Base) { }
上面的例子不起作用,TS 告诉我属性sth 在接口Base 上不存在。由于type 属性上的值'test',我需要更改什么以便TS 理解孩子实际上是Ext 的类型?
【问题讨论】:
标签: typescript