【发布时间】: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