这是indexloc提供的服务,不要输入任何密码
Skip to content

43 - Exclude (🎥 Video Explanation and Solution) #21535

@dimitropoulos

Description

@dimitropoulos

43 - Exclude

Exclude is a very fundamental type to have in your toolbelt in regular day-to-day TypeScript development. It's probably the most re-used utility type in all of the challenges that come, so it's really worthwhile to get familiar with it.

🎥 Video Explanation

Release Date: 2023-01-07 19:00 UTC

Exclude

🔢 Code

import type { Equal, Expect } from "./test-utils";

type A1 = MyExclude<"a" | "b" | "c", "a">;
type B1 = "b" | "c";
type C1 = Expect<Equal<A1, B1>>;
  
type A2 = MyExclude<"a" | "b" | "c", "a" | "b">;
type B2 = "c";
type C2 = Expect<Equal<A2, B2>>;

type A3 = MyExclude<
  string | number | (() => void),
  Function
>;
type B3 = string | number;
type C3 = Expect<Equal<A3, B3>>;

// ============= Your Code Here =============
type MyExclude<T, U> = T extends U ? never : T;

// ================== NOPE ==================
// creates an object type
// when what we want is type unions
type Nope0<T, U> = {
  [P in keyof T as P & U]: T[P];
};

➕ More Solutions

For more video solutions to other challenges: see the umbrella list! #21338

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions