ts如何写数组接口(interface)

// -----------------------------------------------
interface arg {
  [index:number]:[number,string,boolean]
}
const arr :arg = [[1,'1',true]]

for(const i of arr){
  console.log(i)
}
// -----------------------------------------------

// 需要将 arr的类型 [number,string,boolean][] 抽离出来复用
// 但显示TS报错 "message": "类型“arg”不是数组类型或字符串类型,或者没有返回迭代器的 \"[Symbol.iterator]()\" 方法。"
// 目前的解决方案
// interface arg extends Array<[number,string,boolean]> {
//   [index:number]:[number,string,boolean]
// }
// 有没有更好的写法
阅读 11.9k
1 个回答

这里用 type 就行了,不需要 interface 。(类型最好首字母大写)

type Arg = Array<[number,string,boolean]>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题