-
-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathextends-strict.d.ts
More file actions
151 lines (111 loc) · 4.09 KB
/
Copy pathextends-strict.d.ts
File metadata and controls
151 lines (111 loc) · 4.09 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type {IsNever} from './is-never.d.ts';
import type {IsAny} from './is-any.d.ts';
import type {ApplyDefaultOptions} from './internal/object.d.ts';
import type {And} from './and.d.ts';
import type {Or} from './or.d.ts';
import type {IsUnknown} from './is-unknown.d.ts';
import type {OrAll} from './or-all.d.ts';
export type ExtendsStrictOptions = {
/**
Whether to distribute over unions.
@default false
@example
```
import type {ExtendsStrict} from 'type-fest';
type T1 = ExtendsStrict<string | number, string, {distributiveUnions: true}>;
//=> boolean
type T2 = ExtendsStrict<string | number, string, {distributiveUnions: false}>;
//=> false
```
*/
distributiveUnions?: boolean;
/**
Whether `never` extends every other type.
When enabled, `never` is not treated as a bottom type and only extends itself (or `any` / `unknown`).
@default true
@example
```
import type {ExtendsStrict} from 'type-fest';
type T1 = ExtendsStrict<never, number, {strictNever: true}>;
//=> false
type T2 = ExtendsStrict<never, number, {strictNever: false}>;
//=> true
type T3 = ExtendsStrict<never, never, {strictNever: true}>;
//=> true
type T4 = ExtendsStrict<never, any, {strictNever: true}>;
//=> true
type T5 = ExtendsStrict<never, unknown, {strictNever: true}>;
//=> true
```
Note: This option only has an effect when checking assignability from `never` (`ExtendsStrict<never, ...>`), and not when checking assignability to `never` (`ExtendsStrict<..., never>`).
*/
strictNever?: boolean;
/**
Whether `any` extends every other type.
When enabled, `any` does not extend every other type, it only extends itself (or `unknown`).
@default false
@example
```
import type {ExtendsStrict} from 'type-fest';
type T1 = ExtendsStrict<any, number, {strictAny: true}>;
//=> false
type T2 = ExtendsStrict<any, number, {strictAny: false; distributiveUnions: false}>;
//=> true
type T3 = ExtendsStrict<any, any, {strictAny: true}>;
//=> true
type T4 = ExtendsStrict<any, unknown, {strictAny: true}>;
//=> true
```
Note: If `strictAny` is `false` and `distributiveUnions` is `true`, then `any` would distribute and the result would be the `boolean` type, except when checking assignability to `any` or `unknown`.
@example
```
import type {ExtendsStrict} from 'type-fest';
type T1 = ExtendsStrict<any, number, {strictAny: false; distributiveUnions: true}>;
//=> boolean
type T2 = ExtendsStrict<any, any, {strictAny: false; distributiveUnions: true}>;
//=> true
type T3 = ExtendsStrict<any, unknown, {strictAny: false; distributiveUnions: true}>;
//=> true
```
Note: This option only has an effect when checking assignability from `any` (`ExtendsStrict<any, ...>`), and not when checking assignability to `any` (`ExtendsStrict<..., any>`).
*/
strictAny?: boolean;
};
type DefaultExtendsStrictOptions = {
distributiveUnions: false;
strictNever: true;
strictAny: false;
};
/**
A customizable version of `extends` for checking whether one type is assignable to another.
Refer {@link ExtendsStrictOptions} for the different ways you can customize the behavior of this type.
@example
```
import type {ExtendsStrict} from 'type-fest';
type T1 = ExtendsStrict<number | string, string, {distributiveUnions: false}>;
//=> false
type T2 = ExtendsStrict<never, number, {strictNever: true}>;
//=> false
type T3 = ExtendsStrict<any, number, {strictAny: true}>;
//=> false
```
@see {@link ExtendsStrictOptions}
@category Improved Built-in
*/
export type ExtendsStrict<Left, Right, Options extends ExtendsStrictOptions = {}> =
_ExtendsStrict<Left, Right, ApplyDefaultOptions<ExtendsStrictOptions, DefaultExtendsStrictOptions, Options>>;
type _ExtendsStrict<Left, Right, Options extends Required<ExtendsStrictOptions>> =
And<IsAny<Left>, Options['strictAny']> extends true
? Or<IsAny<Right>, IsUnknown<Right>>
: IsNever<Left> extends true
? Options['strictNever'] extends true
? OrAll<[IsNever<Right>, IsAny<Right>, IsUnknown<Right>]>
: true
: Options['distributiveUnions'] extends true
? Left extends Right
? true
: false
: [Left] extends [Right]
? true
: false;
export {};