【问题标题】:TypeScript: An index signature parameter must be a 'string' or 'number' when trying to use string | numberTypeScript:尝试使用字符串时,索引签名参数必须是“字符串”或“数字”|数字
【发布时间】:2019-09-13 20:37:06
【问题描述】:

我正在尝试创建一个函数来规范化我的数组,它需要一个结构如下的输出对象:

{
  allIds: [1],
  byId: {
    1: {...}
  }
}

{
  allIds: ['1'],
  byId: {
    '1': {...}
  }
}

我正在尝试创建一个名为 IOutput 的接口来满足此需求。

我试过了:

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: number | string]: any
  }
}

但它给了我以下错误

索引签名参数类型必须是“字符串”或“数字”。 ts(1023)

当我这样做时它似乎工作:

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: number]: any
  }
}

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: string]: any
  }
}

但这不是我想要完成的。我也试过这个,它给了我同样的错误:

type StringOrNumber = string | number

interface IOutput {
  allIds: string[] | number[]
  byId: {
    [key: StringOrNumber ]: any
  }
}

我怎样才能完成我想做的事情?

【问题讨论】:

    标签: typescript interface normalization


    【解决方案1】:

    这是我们编写索引的当前方式的限制(这将改变soon enough)。索引签名参数只能是 numberstring (正是这些类型,而不是它们的联合,而不是文字类型)。但是,您可以有两个索引签名,一个用于number,一个用于string

    还有一个小技巧,如果你有string 签名,你实际上也可以通过number 来索引。所以这意味着如果string 索引和number 索引具有相同的返回类型,你只需要字符串索引

    interface IOutput {
        allIds: string[] | number[]
        byId: {
            [key: string]: any
            // [key: number]: any // Valid but not necessary
        }
    }
    
    let o: IOutput = {
        allIds: [1],
        byId: {
            1: {}
        }
    }
    let o2: IOutput = {
        allIds: ['1'],
        byId: {
            '1': {}
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2020-05-05
      • 2017-05-06
      • 2019-11-04
      • 1970-01-01
      • 2020-09-17
      • 2020-10-27
      相关资源
      最近更新 更多