Case 1: No errors detected when "randomKey" is passed down to doMethod.
interface Options {
bar: string;
foo?: number;
}
function doMethod(options: Options) {
console.log(options);
}
const options = {
bar: 'bar',
foo: 123,
randomKey: 231,
}
doMethod(options); // No errors appear!
console.log(options);
Case 2: Errors only appears after when options are directly passed into the function
interface Options {
bar: string;
foo?: number;
}
function doMethod(options: Options) {
console.log(options);
}
doMethod({
bar: 'bar',
foo: 123,
randomKey: 231, // ERROR: "Object literal may only specify known properties, and 'randomKey' does not exist in type 'Options'."
});
console.log(options); // In this scenario, I cannot console log options
The silent error is tough to find. You can easily misspell a word for example, and things might not happen they way you want it.
What happens when you misspell "foo", like "fooo"? The silent error would still occur. You expect "foo" to trigger something but would not in this case if you spelt "fooo".