我有一个关于 Typescript 基础知识的问题。
考虑这个例子:
// 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 …Run Code Online (Sandbox Code Playgroud)