Typescript currently does not have a concept of exact types (there is exception for object literals, I'll explain in the end). In other words, every object of some interface can have extra properties (which don't exist in the interface).
Hence this object:
{
command: 34234,
children: [{ foo: 'qwerty' }, { asdf: 'zxcv' }],
}
satisfies Type1
interface because it has all of its properties (command
) property but it also satisfies Type2
interface because it has all of its properties (children
) as well.
A note on object literals:
typescript does implement a concept of exact type only for object literals:
export const unionType: Type1 = {
command: 34234,
children: [{ foo: 'qwerty' }, { asdf: 'zxcv' }], // oops, not allowed here
};
export const unionType: Type2 = {
command: 34234, // oops, not allowed here
children: [{ foo: 'qwerty' }, { asdf: 'zxcv' }],
};
Above is the code which demonstrates object literals, and below is the code without them:
const someVar = {
command: 34234,
children: [{ foo: 'qwerty' }, { asdf: 'zxcv' }]
};
const: Type1 = someVar // ok, because assigning not an object literal but a variable