TypeScript provides the ability to check for unknown properties. Consider the code snippet below:
interface MyInterface {
key: string
}
const myVar: MyInterface = {
asda: 'asdfadf'
}
The code above will result in an error:
Type '{ asda: string; }' is not assignable to type 'MyInterface'.
Object literal may only specify known properties, and 'asda' does not exist in type 'MyInterface'.
On the other hand, the following code will compile successfully. An empty interface can accept any value
interface EmptyInterface {
}
const myVar: EmptyInterface = {
asda: 'asdfadf'
}
But what if you want to define a type for an empty object that may have no properties at all? How can you achieve this in TypeScript?