【问题标题】:How to specify the following type in typescript?如何在打字稿中指定以下类型?
【发布时间】:2021-01-04 08:36:14
【问题描述】:

我想创建一个接口,其中我们有一个名为sortedTabs 的变量。变量sortedTabs 是对象类型,其中可以包含任意多个数组,但每个数组都具有相同的类型tabsAn[],但我将如何指定该类型?我如何告诉 TypeScript 我期望一个对象包含任意数量的 tabsAn[] 类型的数组?

export interface Initialization{
    sortedTabs: // how to specify the object described above?
}

【问题讨论】:

  • 听起来像你想要的{ [key: string]: tabsAn[] }
  • @AluanHaddad 它还会是一个对象吗?因为我需要对象中的键来指定应该使用哪个数组。这种类型在我的界面中会是什么样子?编辑:谢谢,我会试试的!
  • 看起来像interface I { [key: string]: tabsAn[] }

标签: typescript


【解决方案1】:

有两种方式,第一种是index type

export interface Initialization {
    sortedTabs: { [key: string]: tabsAn[] }
}

第二个是Record utility type,它实际上只是索引类型的包装:

export interface Initialization {
    sortedTabs: Record<string, tabsAn[]>
}

【讨论】:

    猜你喜欢
    • 2017-02-20
    • 2021-06-02
    • 2015-11-06
    • 2017-01-16
    • 2021-12-30
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多