【发布时间】:2019-01-21 07:58:57
【问题描述】:
我有 3 个接口:
interface IA {
field1: string
}
interface IB extends A {
field2: number
}
interface IC {
test: string;
myInput: IA
}
还有一个功能:
test(params: IC) {
...
}
我希望我的方法能够在myInput入口接口接收IB,但是,我只能通过接口IA,即使IB是从它派生的。
我收到的错误:
error TS2322: Type '{ test: string; myInput: IA }' is not assignable to type 'IA'.
Object literal may only specify known properties, and 'test' does not exist in type 'IA'.
我尝试使用类似的东西来定义 myInput :
T<T extends IA> 但无法编译。
【问题讨论】:
-
这不是这里发生的事情。该错误表明您正在尝试将
IC的对象分配给某处的IA。
标签: typescript extends