Today I Learned

hashrocket A Hashrocket project

Omitting specific fields in TypeScript types

TypeScript has an Omit utility type that lets you create a new type by excluding specific keys from an existing type. It's perfect for cases where you need a type that's almost the same but without certain properties.

Here's an example:

type User = {
  id: number;
  name: string;
  password: string;
};

type PublicUser = Omit<User, 'password'>;

Now we have a PublicUser type that includes id and name, but the password field is excluded.

h/t Vinicius Negrisolo

See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.