This behavior can be frustrating when you misspell the names of an optional parameter. Imagine you misspell timeout as timeOut:
const fetch = (options: { url: string; timeout?: number }) => {
// Implementation
};
const options = {
url: "/",
timeOut: 1000,
};
fetch(options); // No error!In this case, TypeScript won’t show an error, and you won’t get the runtime behavior you expect. The only way to source the error would be to provide a type annotation for the options object:
const options: { timeout?: number; url: string } = {
url: "/",
timeOut: 1000, // Red squiggly line under timeOut
};Now, we’re comparing an inline object to a type, and TypeScript will check for excess properties.