I found that function accept a value which has different type with argument in typescript.
type A = { name: string, age: number };
type B = { name: string, age: number, hobby: 'cleanup' | 'pingpong' }
function test(t: A) {
console.log(t);
}
const a: A = { name: 'yoon', age: 28 };
const b: B = { name: 'taetae', age: 25, hobby: 'cleanup' };
test(a); // OK
test(b); // OK
test({ name: 'yoon', age: 28 }); // OK
test({ name: 'yoon', age: 28, hobby: 'pingpong' }); // ERROR. Why?
I want to know why test(b)
is OK but test({ name: 'yoon', age: 28, hobby: 'pingpong' })
is not.
Edit
In what case this policy (excess property check) is useful ?