TS 中interface 如何定义?

allPoints 里的属性是累加的,个数不确定:

allPoints = {
  0:[{ x: 0, y: 0 }],
  ...,
  100:[{ x: 0, y: 0 }],
  ...
}

这样要如何定义 interface 呢?

allPoints = [
  [{ x: 0, y: 0 }],
  ... 
]

换成数组嘞?

阅读 1.5k
2 个回答
interface ThePoint {
    x: number
    y: number
}

type AllPoints = Array<Array<ThePoint>>

const points: AllPoints = [
    [{ x: 0, y: 0 }],
    [{ x: 0, y: 0 }],
    [{ x: 100, y: 6 }]
]

interface SomeWayPoints {
    [index: number]: Array<ThePoint>
}
const some: SomeWayPoints = {
    15: [{ x: 100, y: 6 }],
    23333: [{ x: 1, y: 47 }]
}

for (const list of points) {
    for (const p of list) {
        console.log(p.x, p.y)
    }
}

这样?

interface Points {
  [key: number]: { x: number; y: number }[];
}

const allPoints:Points = {
  0:[{ x: 0, y: 0 }],
  1:[{ x: 0, y: 0 }],
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏