【问题标题】:How to use three-dot to do destructuring for types in typescript如何使用三点对打字稿中的类型进行解构
【发布时间】:2021-12-03 05:43:44
【问题描述】:

在流程中我通常使用以下内容

type A = {|
  a: string
|}
// in another file, import type A
type B = {|
  ...A,
  b: string,
|}

但是当我在 typescripts 中做类似的事情时,它给了我 `Member 'A' 隐含一个 'any' type.ts(7008)

type A = {
  a: string
}
// in another file, import type A
type B = {
  ...A,
  b: string,
}

我该怎么做才能做到这一点?谢谢。

【问题讨论】:

    标签: typescript flowtype


    【解决方案1】:

    使用交集类型:

    type B = A & {b: string}
    

    或者你可以使用接口:

    interface B extends A {
        b: string
    }
    

    不同之处在于接口可以通过其他地方的其他interface B 声明添加字段,而type B = ... 在声明后无法修改。详情请见the docs

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多