deno.land / std@0.173.0 / testing / types_test.ts

نووسراو ببینە
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.// Copyright @dsherret and dsherret/conditional-type-checks contributors. All rights reserved. MIT license.// deno-lint-ignore-file no-explicit-any ban-types
import { Assert, AssertFalse, AssertTrue, assertType, Has, IsAny, IsExact, IsNever, IsNullable, IsUnknown, NotHas,} from "./types.ts";
// IsNullable{ // matching assertType<IsNullable<string | null>>(true); assertType<IsNullable<string | undefined>>(true); assertType<IsNullable<null | undefined>>(true); // maybe this shouldn't be true?
// not matching assertType<IsNullable<string>>(false); assertType<IsNullable<any>>(false); assertType<IsNullable<never>>(false); assertType<IsNullable<unknown>>(false);}
// IsExact{ // matching assertType<IsExact<string | number, string | number>>(true); assertType<IsExact<string | number | Date, string | number | Date>>(true); assertType<IsExact<string | undefined, string | undefined>>(true); assertType<IsExact<any, any>>(true); // ok to have any for both assertType<IsExact<unknown, unknown>>(true); assertType<IsExact<never, never>>(true); assertType<IsExact<{}, {}>>(true); assertType<IsExact<{ prop: string }, { prop: string }>>(true); assertType<IsExact<{ prop: { prop: string } }, { prop: { prop: string } }>>( true, ); assertType<IsExact<{ prop: never }, { prop: never }>>(true); assertType<IsExact<{ prop: any }, { prop: any }>>(true); assertType<IsExact<{ prop: unknown }, { prop: unknown }>>(true); assertType<IsExact<typeof globalThis, typeof globalThis>>(true);
// not matching assertType<IsExact<string | number | Date, string | number>>(false); assertType<IsExact<string, string | number>>(false); assertType<IsExact<string | undefined, string>>(false); assertType<IsExact<string | undefined, any | string>>(false); assertType<IsExact<any | string | undefined, string>>(false); assertType<IsExact<string, any>>(false); assertType<IsExact<string, unknown>>(false); assertType<IsExact<string, never>>(false); assertType<IsExact<never, never | string>>(false); assertType<IsExact<unknown, any>>(false); assertType<IsExact<never, any>>(false); assertType<IsExact<Date | typeof globalThis, Date>>(false); assertType<IsExact<{ name: string; other?: Date }, { name: string }>>(false); assertType<IsExact<{ prop: Date }, { prop: string }>>(false); assertType<IsExact<{ other?: Date }, { prop?: string }>>(false); assertType<IsExact<{ prop: { prop?: string } }, { prop: { prop: string } }>>( false, ); assertType<IsExact<{ prop: any }, { prop: string }>>(false); assertType<IsExact<{ prop: any }, { prop: unknown }>>(false); assertType<IsExact<{ prop: any }, { prop: never }>>(false); assertType<IsExact<{ prop: unknown }, { prop: never }>>(false); assertType<IsExact<{ prop: { prop: unknown } }, { prop: { prop: any } }>>( false, ); assertType<IsExact<{ prop: { prop: unknown } }, { prop: { prop: never } }>>( false, ); assertType<IsExact<{ prop: { prop: any } }, { prop: { prop: never } }>>( false, ); assertType<IsExact<{ prop: string }, { prop: never }>>(false); assertType<IsExact<{ prop: { prop: any } }, { prop: { prop: string } }>>( false, ); assertType< IsExact< { prop: any } | { prop: string }, { prop: number } | { prop: string } > >(false); assertType<IsExact<{ prop: string | undefined }, { prop?: string }>>(false); // these are different}
// Has{ // matching assertType<Has<string | number, string>>(true); assertType<Has<number, number>>(true); assertType<Has<string | number, Date | string>>(true); // maybe? assertType<Has<any, number>>(true); assertType<Has<any, any>>(true); assertType<Has<any, unknown>>(true); assertType<Has<any, never>>(true);
// not matching assertType<Has<string | number, Date>>(false); assertType<Has<string, number>>(false); assertType<Has<number, any>>(false); assertType<Has<unknown, any>>(false); assertType<Has<never, any>>(false);}
// NotHas{ // matching assertType<NotHas<string | number, Date>>(true); assertType<NotHas<string, number>>(true);
// not matching assertType<NotHas<string | number, string>>(false); assertType<NotHas<number, number>>(false); assertType<NotHas<string | number, Date | string>>(false); // should be true?}
// IsAny{ // matching assertType<IsAny<any>>(true);
// not matching assertType<IsAny<string>>(false); assertType<IsAny<unknown>>(false); assertType<IsAny<never>>(false);
// tests for issue #3 (IsAny resolving to boolean) assertType<IsExact<IsAny<string>, false>>(true); assertType<IsExact<IsAny<5>, false>>(true);}
// IsNever{ // matching assertType<IsNever<never>>(true);
// not matching assertType<IsNever<string>>(false); assertType<IsNever<any>>(false); assertType<IsNever<unknown>>(false);}
// IsUnknown{ // matching assertType<IsUnknown<unknown>>(true);
// not matching assertType<IsUnknown<string>>(false); assertType<IsUnknown<any>>(false); assertType<IsUnknown<never>>(false);}
// AssertTrue{ type test = AssertTrue<IsNever<never>>;}
// AssertFalse{ type test = AssertFalse<IsNever<string>>;}
// Assert{ type test = | Assert<Has<string | number, number>, true> | Assert<Has<string | number, Date>, false>;}
// Recursive types{ type RecursiveType1 = string | number | Date | RecursiveType1[]; assertType<IsExact<RecursiveType1, RecursiveType1>>(true); type RecursiveType2 = { a: string; prop: RecursiveType2; sub: { prop: RecursiveType2; other: RecursiveType1; }; }; assertType<IsExact<RecursiveType2, RecursiveType2>>(true); assertType<IsExact<RecursiveType1, RecursiveType2>>(false);}
std

Version Info

Tagged at
a year ago