首页 > 解决方案 > 索引签名参数类型必须是“字符串”或“数字”

问题描述

为什么会出现以下界面

export interface Interface<T> {
    [id:number | string]: T[]
}

抛出错误

索引签名参数类型必须是“字符串”或“数字”

标签: typescript

解决方案


你试过把它们分开吗?

export interface Interface<T> {
  [id: string]: T[];
  [id: number]: T[];
}

// And then using it...
const nums: Interface<number> = {
  one: [1, 2],
  2: [2, 3]
};

推荐阅读