I have a question about the basics of Typescript.
Consider this example:
// Interface 1
interface Empty {}
// Interface 2
interface Single {
a: number;
}
function printEmpty(arg: Empty): void {
console.log(`Hello World.`);
}
function printSingle(arg: Single): void {
console.log(`Hello World, a: ${arg.a} `)
}
const obj = {a: 1, b: 2};
// ===================== Tests =====================
// TEST 1
printEmpty({a: 1, b: 2}); // valid
// TEST 2
// Q1: if the above is valid, why is this invalid
printSingle({a: 1, b: 2}) // invalid!
// TEST 3
// Q2: if the above is invalid, why is this valid
printSingle(obj); // valid
The error I get is
Argument of type
'{ a: number; b: number; }'is not assignable to parameter of type'Single'. Object literal may only specify known properties, and'b'does not exist in type'Single'.(2345)
Why does TEST 2 fail but TEST 1 and TEST 3 pass?
Here's a reproduction of this error on the official TS playground: [LINK]