【问题标题】:can't use interface which extends another interface as parameter to method不能使用扩展另一个接口的接口作为方法的参数
【发布时间】: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'.

我尝试使用类似的东西来定义 myInputT<T extends IA> 但无法编译。

【问题讨论】:

  • 这不是这里发生的事情。该错误表明您正在尝试将IC 的对象分配给某处的IA

标签: typescript extends


【解决方案1】:

我像this 一样尝试过,似乎一切正常。 打开控制台,查看结果。

【讨论】:

  • 这并不能解决这个问题,因为您无法接收其他对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2012-04-30
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多