I tried out the following code snippet here
interface OnlyName {
name: string
}
interface MyTest2 extends OnlyName {
age: number
}
let test1: OnlyName;
const setTest1 = (v: OnlyName) => {
test1 = v
console.log(test1)
}
let test2: MyTest2 = {
name: 'test2',
age: 2,
}
setTest1(test2)
I was expecting an error when calling setTest1() because it is supposed to only accept parameters of type OnlyName
. However, to my surprise, it worked even when passing MyTest2
.
Why did it not throw an error and is there a way to enforce only accepting OnlyName
?