typescript - 是 :[Interface] a valid array definition in Typescript?

标签 typescript

我的 typescript 代码库中有一堆这样的代码......

export interface SomeType {
    name: string;
}

export interface SomeComposedType {
    things: [SomeType];
}
这一直工作正常,但后来我开始遇到问题

Property '0' is missing in type



Argument of type 'SomeType[]' is not assignable to parameter of type '[SomeType]'


我现在真的很困惑这个。我很确定
let x:SomeType[] = []
相当于
let x: Array<SomeType> = []
但是
let x:[SomeType] = []
也等价和正确?

最佳答案

[SomeType]代表一个 tuple type ,即一个只有 SomeType 一个元素的数组[string, number]例如,将匹配一个数组,如 ["test", 0]

关于typescript - 是 :[Interface] a valid array definition in Typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63625736/

相关文章:

angular - 将 Ionic 应用程序添加到 Rails 4 应用程序

reactjs - 如何在 Material-UI 中扩展 OverridableComponent 接口(interface)

javascript - 如何从数组中获取匹配元素并更改数组对象?

javascript - 我无法访问两个对象中的 id

angular - 我可以在 typescript 中获取通用类型的元数据吗?

javascript - 在 Node 服务器中使用 JQuery - TypeScript

typescript - 为什么 typescript 将联合中的属性标记为不存在?

angular - 如何根据角色启用组件/操作?

reactjs - React State 未从 prop 初始化

javascript - 为什么我不能在@NgModule 中导入 Angular 2 服务?