-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathexclude-strict.d.ts
More file actions
51 lines (41 loc) · 1.74 KB
/
exclude-strict.d.ts
File metadata and controls
51 lines (41 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
A stricter version of {@link Exclude<T, U>} that ensures every member of `U` can successfully exclude something from `T`.
For example, `ExcludeStrict<string | number | boolean, number | bigint>` will error because `bigint` cannot exclude anything from `string | number | boolean`.
@example
```
// Valid Examples
import type {ExcludeStrict} from 'type-fest';
type Example1 = ExcludeStrict<{status: 'success'; data: string[]} | {status: 'error'; error: string}, {status: 'success'}>;
//=> {status: 'error'; error: string}
type Example2 = ExcludeStrict<'xs' | 's' | 'm' | 'l' | 'xl', 'xs' | 's'>;
//=> 'm' | 'l' | 'xl'
type Example3 = ExcludeStrict<{x: number; y: number} | [number, number], unknown[]>;
//=> {x: number; y: number}
```
@example
```
// Invalid Examples
import type {ExcludeStrict} from 'type-fest';
// `'xxl'` cannot exclude anything from `'xs' | 's' | 'm' | 'l' | 'xl'`
// @ts-expect-error
type Example1 = ExcludeStrict<'xs' | 's' | 'm' | 'l' | 'xl', 'xl' | 'xxl'>;
// ~~~~~~~~~~~~
// Error: Type "'xl' | 'xxl'" does not satisfy the constraint 'never'.
// `unknown[]` cannot exclude anything from `{x: number; y: number} | {x: string; y: string}`
// @ts-expect-error
type Example2 = ExcludeStrict<{x: number; y: number} | {x: string; y: string}, unknown[]>;
// ~~~~~~~~~
// Error: Type 'unknown[]' does not satisfy the constraint 'never'.
```
@category Improved Built-in
*/
export type ExcludeStrict<
T,
U extends [U] extends [
// Ensure every member of `U` excludes something from `T`
U extends unknown ? ([T] extends [Exclude<T, U>] ? never : U) : never,
]
? unknown
: never,
> = Exclude<T, U>;
export {};