Dhananjay Patel Logo
  1. Home
  2. / Blog
  3. / Typescript Api
  4. / Lessons
  5. / 5

Next

Exclude

  • Exclude creates a new type by removing certain types from a union.
  • It’s useful when you want to restrict the types that can be used in a specific context.
type Event = 'click' | 'scroll' | 'mousemove';
type NonScrollEvent = Exclude<Event, 'scroll'>; // 'click' | 'mousemove'
const handleEvent = (event: NonScrollEvent) => {
console.log(`Handling event: ${event}`);
};
handleEvent('click'); // OK
// handleEvent('scroll'); // Error: Argument of type '"scroll"' is not assignable to parameter of type 'NonScrollEvent'.

Next