【发布时间】:2019-07-12 16:19:19
【问题描述】:
下面是一个使用联合类型的函数。
type Shape =
{ kind: 'circle', radius: number } |
{ kind: 'rectangle', w: number, h: number }
const getArea = (shape: Shape) => { // this works
switch(shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2
case 'rectangle':
return shape.w * shape.h
}
throw new Error('Invalid shape')
}
getArea({ kind: 'circle', radius: 10 })
它接受参数作为对象,因此它可以工作。
但是当函数不接受参数作为对象时,我该怎么做。请参考以下示例
type Shape =
{ (kind: 'circle', ...args: [number]) } |
{ (kind: 'rectangle', ...args: [number, number]) }
const getArea: Shape = (kind, ...args) => { // this doesn't work
switch(kind) {
case 'circle':
return Math.PI * args[0] ** 2
case 'rectangle':
return args[0] * args[1]
}
throw new Error('Invalid shape')
}
getArea('circle', 10)
getArea('rectangle', 5, 5)
另外,我可以这样写
const getArea: Shape = (kind: 'update' | 'reset', ...args: [number] | [number, number]) => {
但它不会给我完整的类型安全性。如第一个示例所示,我希望在 kind 更改时更改 args。
或者对于这类问题有什么不同的方法?
【问题讨论】:
标签: typescript types union-types