I have come across a peculiar situation that should theoretically result in the TS compiler throwing an error, yet it does not. I am hoping someone can shed some light on why this is happening.
In the following code snippet, I pass an interface called Foo
to a function named frobnicator
, which accepts it without any issues. Inside the body of frobnicator
, I remove the field bar.y
. Surprisingly, after the execution of frobnicator
, the type system allows me to access and print bar.y
without encountering any errors, even though y
has been removed from the object.
Shouldn't the type system prevent me from passing foo
to frobnicator
considering that now foo
no longer adheres to the Foo
interface as expected by TypeScript?
interface Foo {
bar: { x: number, y: number };
}
function frobnicator(thing: { bar: { x: number } }) {
thing.bar = { x: 1 }; // "remove" bar.y
}
const foo: Foo = { bar: { x: 1, y: 2 } };
frobnicator(foo); // The implementation "removes" bar.y
console.log(foo.bar.y); // TypeScript does not error despite bar.y missing