typescript, 是否可以定义一个name为string
, 其他属性为number
的对象?
interface A {
name: string,
[key: string]: number
}
想当然了, 这是官方文档中提到的错误:
请问有什么方法可以做到吗?
###ts 版本 4.1.3
type IFoo = {
name: string
} | {
[key : string]: number
}
const a:IFoo = {
name: '13'
}
const b:IFoo = {
age: 99,
name: 'qwer'
}
fix: 声明的时候没问题, 但是使用的时候会有问题, 暂无其他方案