Skip to content

Commit f1d7afe

Browse files
authored
Merge pull request #21847 from Microsoft/predefinedConditionalTypes
Predefined conditional types
2 parents 49e78f6 + 11a075c commit f1d7afe

11 files changed

+1140
-1141
lines changed

src/lib/es5.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,31 @@ type Record<K extends string, T> = {
13381338
[P in K]: T;
13391339
};
13401340

1341+
/**
1342+
* Exclude from T those types that are assignable to U
1343+
*/
1344+
type Exclude<T, U> = T extends U ? never : T;
1345+
1346+
/**
1347+
* Extract from T those types that are assignable to U
1348+
*/
1349+
type Extract<T, U> = T extends U ? T : never;
1350+
1351+
/**
1352+
* Exclude null and undefined from T
1353+
*/
1354+
type NonNullable<T> = T extends null | undefined ? never : T;
1355+
1356+
/**
1357+
* Obtain the return type of a function type
1358+
*/
1359+
type ReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : any;
1360+
1361+
/**
1362+
* Obtain the return type of a constructor function type
1363+
*/
1364+
type InstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R ? R : any;
1365+
13411366
/**
13421367
* Marker for contextual 'this' type
13431368
*/

tests/baselines/reference/conditionalTypes1.errors.txt

+43-47
Large diffs are not rendered by default.

tests/baselines/reference/conditionalTypes1.js

+26-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
//// [conditionalTypes1.ts]
2-
type Diff<T, U> = T extends U ? never : T;
3-
type Filter<T, U> = T extends U ? T : never;
4-
type NonNullable<T> = Diff<T, null | undefined>;
2+
type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
3+
type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "a" | "c"
54

6-
type T00 = Diff<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
7-
type T01 = Filter<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "a" | "c"
8-
9-
type T02 = Diff<string | number | (() => void), Function>; // string | number
10-
type T03 = Filter<string | number | (() => void), Function>; // () => void
5+
type T02 = Exclude<string | number | (() => void), Function>; // string | number
6+
type T03 = Extract<string | number | (() => void), Function>; // () => void
117

128
type T04 = NonNullable<string | number | undefined>; // string | number
139
type T05 = NonNullable<(() => string) | string[] | null | undefined>; // (() => string) | string[]
@@ -31,23 +27,23 @@ function f3<T>(x: Partial<T>[keyof T], y: NonNullable<Partial<T>[keyof T]>) {
3127

3228
type Options = { k: "a", a: number } | { k: "b", b: string } | { k: "c", c: boolean };
3329

34-
type T10 = Diff<Options, { k: "a" | "b" }>; // { k: "c", c: boolean }
35-
type T11 = Filter<Options, { k: "a" | "b" }>; // { k: "a", a: number } | { k: "b", b: string }
30+
type T10 = Exclude<Options, { k: "a" | "b" }>; // { k: "c", c: boolean }
31+
type T11 = Extract<Options, { k: "a" | "b" }>; // { k: "a", a: number } | { k: "b", b: string }
3632

37-
type T12 = Diff<Options, { k: "a" } | { k: "b" }>; // { k: "c", c: boolean }
38-
type T13 = Filter<Options, { k: "a" } | { k: "b" }>; // { k: "a", a: number } | { k: "b", b: string }
33+
type T12 = Exclude<Options, { k: "a" } | { k: "b" }>; // { k: "c", c: boolean }
34+
type T13 = Extract<Options, { k: "a" } | { k: "b" }>; // { k: "a", a: number } | { k: "b", b: string }
3935

40-
type T14 = Diff<Options, { q: "a" }>; // Options
41-
type T15 = Filter<Options, { q: "a" }>; // never
36+
type T14 = Exclude<Options, { q: "a" }>; // Options
37+
type T15 = Extract<Options, { q: "a" }>; // never
4238

43-
declare function f4<T extends Options, K extends string>(p: K): Filter<T, { k: K }>;
39+
declare function f4<T extends Options, K extends string>(p: K): Extract<T, { k: K }>;
4440
let x0 = f4("a"); // { k: "a", a: number }
4541

46-
type OptionsOfKind<K extends Options["k"]> = Filter<Options, { k: K }>;
42+
type OptionsOfKind<K extends Options["k"]> = Extract<Options, { k: K }>;
4743

4844
type T16 = OptionsOfKind<"a" | "b">; // { k: "a", a: number } | { k: "b", b: string }
4945

50-
type Select<T, K extends keyof T, V extends T[K]> = Filter<T, { [P in K]: V }>;
46+
type Select<T, K extends keyof T, V extends T[K]> = Extract<T, { [P in K]: V }>;
5147

5248
type T17 = Select<Options, "k", "a" | "b">; // // { k: "a", a: number } | { k: "b", b: string }
5349

@@ -332,13 +328,10 @@ function f33() {
332328

333329

334330
//// [conditionalTypes1.d.ts]
335-
declare type Diff<T, U> = T extends U ? never : T;
336-
declare type Filter<T, U> = T extends U ? T : never;
337-
declare type NonNullable<T> = Diff<T, null | undefined>;
338-
declare type T00 = Diff<"a" | "b" | "c" | "d", "a" | "c" | "f">;
339-
declare type T01 = Filter<"a" | "b" | "c" | "d", "a" | "c" | "f">;
340-
declare type T02 = Diff<string | number | (() => void), Function>;
341-
declare type T03 = Filter<string | number | (() => void), Function>;
331+
declare type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">;
332+
declare type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">;
333+
declare type T02 = Exclude<string | number | (() => void), Function>;
334+
declare type T03 = Extract<string | number | (() => void), Function>;
342335
declare type T04 = NonNullable<string | number | undefined>;
343336
declare type T05 = NonNullable<(() => string) | string[] | null | undefined>;
344337
declare function f1<T>(x: T, y: NonNullable<T>): void;
@@ -354,40 +347,40 @@ declare type Options = {
354347
k: "c";
355348
c: boolean;
356349
};
357-
declare type T10 = Diff<Options, {
350+
declare type T10 = Exclude<Options, {
358351
k: "a" | "b";
359352
}>;
360-
declare type T11 = Filter<Options, {
353+
declare type T11 = Extract<Options, {
361354
k: "a" | "b";
362355
}>;
363-
declare type T12 = Diff<Options, {
356+
declare type T12 = Exclude<Options, {
364357
k: "a";
365358
} | {
366359
k: "b";
367360
}>;
368-
declare type T13 = Filter<Options, {
361+
declare type T13 = Extract<Options, {
369362
k: "a";
370363
} | {
371364
k: "b";
372365
}>;
373-
declare type T14 = Diff<Options, {
366+
declare type T14 = Exclude<Options, {
374367
q: "a";
375368
}>;
376-
declare type T15 = Filter<Options, {
369+
declare type T15 = Extract<Options, {
377370
q: "a";
378371
}>;
379-
declare function f4<T extends Options, K extends string>(p: K): Filter<T, {
372+
declare function f4<T extends Options, K extends string>(p: K): Extract<T, {
380373
k: K;
381374
}>;
382375
declare let x0: {
383376
k: "a";
384377
a: number;
385378
};
386-
declare type OptionsOfKind<K extends Options["k"]> = Filter<Options, {
379+
declare type OptionsOfKind<K extends Options["k"]> = Extract<Options, {
387380
k: K;
388381
}>;
389382
declare type T16 = OptionsOfKind<"a" | "b">;
390-
declare type Select<T, K extends keyof T, V extends T[K]> = Filter<T, {
383+
declare type Select<T, K extends keyof T, V extends T[K]> = Extract<T, {
391384
[P in K]: V;
392385
}>;
393386
declare type T17 = Select<Options, "k", "a" | "b">;

0 commit comments

Comments
 (0)