【发布时间】:2018-08-11 19:42:08
【问题描述】:
TypeScript 允许检查未知属性。以下
interface MyInterface {
key: string
}
const myVar: MyInterface = {
asda: 'asdfadf'
}
将失败,
类型 '{ asda: string; }' 不可分配给类型 'MyInterface'。
对象字面量只能指定已知属性,而 'asda' 不 存在于“MyInterface”类型中。
但是,此语句将毫无问题地编译。 Empty interface will accept any value
interface EmptyInterface {
}
const myVar: EmptyInterface = {
asda: 'asdfadf'
}
但是,如果我真的想为可能没有 任何 属性的空对象定义类型怎么办?我怎样才能在打字稿中做到这一点?
【问题讨论】:
标签: typescript interface