Why is this valid:
type A = {
a: string;
};
function withA(a: A) {}
const test = (message: {b: number}) => {
withA({
...message, // this
a: 'foo',
});
};
and this is valid:
type A = {
a: string;
};
function withA(a: A) {}
const test = <T>(message: T) => {
withA({
...message,
b: 'bar', // this
a: 'foo',
});
};
I don't understand why using the spread operator makes this possible.
I tried reading documentation about structural typing and exact types but I can't find a precise answer.
Edit: My real question is why we have no warning here, like "Object literal may only specify known properties, and 'b' does not exist in type 'A'". If I remove the spread line I do have a warning
b
: Playground Link