简体   繁体   English

如何使用三点对打字稿中的类型进行解构

[英]How to use three-dot to do destructuring for types in typescript

in flow I usually use the following在流程中我通常使用以下

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

But when I'm doing similar in typescripts, it gives me `Member 'A' implicitly has an 'any' type.ts(7008)但是当我在打字稿中做类似的事情时,它给了我`Member 'A' 隐含地有一个 'any' type.ts(7008)

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

what can I do to make this right?我该怎么做才能做到这一点? Thanks.谢谢。

Use an intersection type:使用交集类型:

type B = A & {b: string}

Or you can use interfaces:或者你可以使用接口:

interface B extends A {
    b: string
}

The difference is that an interface can have fields added to it by other interface B declarations elsewhere, whereas type B = ... cannot be modified after it is declared.不同之处在于接口可以在别处的其他interface B声明中添加字段,而type B = ...在声明后不能修改。 See the docs for more details.有关更多详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM