Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mattpocock/a0811089b0721e6c80059efb2d8e4561 to your computer and use it in GitHub Desktop.

Select an option

Save mattpocock/a0811089b0721e6c80059efb2d8e4561 to your computer and use it in GitHub Desktop.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment