Reputation: 38341
TypeScript allows checking for checking unknown properties. The following
interface MyInterface {
key: string
}
const myVar: MyInterface = {
asda: 'asdfadf'
}
will fails with
Type '{ asda: string; }' is not assignable to type 'MyInterface'.
Object literal may only specify known properties, and 'asda' does not exist in type 'MyInterface'.
However, this statement will compile without any issue. Empty interface will accept any value
interface EmptyInterface {
}
const myVar: EmptyInterface = {
asda: 'asdfadf'
}
However, what if I actually want to define type for an empty object that may not have any properties? How can I accomplish that in typescript?
Upvotes: 4
Views: 5402
Reputation: 249466
To define an interface that never has any members, you can define an indexer that returns never
interface None { [n: string]: never }
// OK
let d2 : None = {
}
let d3 : None = {
x: "" // error
}
Upvotes: 9