File: indexingTypesWithNever.types

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (283 lines) | stat: -rw-r--r-- 7,187 bytes parent folder | download | duplicates (3)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
=== tests/cases/compiler/indexingTypesWithNever.ts ===
type TestObj = {
>TestObj : { a: string; b: number; }

  a: string;
>a : string

  b: number;
>b : number

};

// Should be never but without an error
type Result1 = TestObj[never];
>Result1 : never

type EmptyObj = {};
>EmptyObj : {}

// Should be never but without an error
type Result2 = EmptyObj[keyof EmptyObj];
>Result2 : never

declare function genericFn1<T>(obj: T): T[never];
>genericFn1 : <T>(obj: T) => T[never]
>obj : T

// Should be never
const result3 = genericFn1({ c: "ctest", d: "dtest" });
>result3 : never
>genericFn1({ c: "ctest", d: "dtest" }) : never
>genericFn1 : <T>(obj: T) => T[never]
>{ c: "ctest", d: "dtest" } : { c: string; d: string; }
>c : string
>"ctest" : "ctest"
>d : string
>"dtest" : "dtest"

declare function genericFn2<T extends { [ind: string]: string }>(
>genericFn2 : <T extends { [ind: string]: string; }>(obj: T) => T[never]
>ind : string

  obj: T
>obj : T

): T[never];

// Should be never
const result4 = genericFn2({ e: "etest", f: "ftest" });
>result4 : never
>genericFn2({ e: "etest", f: "ftest" }) : never
>genericFn2 : <T extends { [ind: string]: string; }>(obj: T) => T[never]
>{ e: "etest", f: "ftest" } : { e: string; f: string; }
>e : string
>"etest" : "etest"
>f : string
>"ftest" : "ftest"

declare function genericFn3<
>genericFn3 : <T extends { [K in keyof T]: T[K]; }, U extends keyof T, V extends keyof T>(obj: T, u: U, v: V) => T[U & V]

  T extends { [K in keyof T]: T[K] },
  U extends keyof T,
  V extends keyof T
>(obj: T, u: U, v: V): T[U & V];
>obj : T
>u : U
>v : V

// Should be never
const result5 = genericFn3({ g: "gtest", h: "htest" }, "g", "h"); // 'g' & 'h' will reduce to never
>result5 : never
>genericFn3({ g: "gtest", h: "htest" }, "g", "h") : never
>genericFn3 : <T extends { [K in keyof T]: T[K]; }, U extends keyof T, V extends keyof T>(obj: T, u: U, v: V) => T[U & V]
>{ g: "gtest", h: "htest" } : { g: string; h: string; }
>g : string
>"gtest" : "gtest"
>h : string
>"htest" : "htest"
>"g" : "g"
>"h" : "h"


declare const obj: {a: string, b: number}
>obj : { a: string; b: number; }
>a : string
>b : number

declare const key: never
>key : never

const result6 = obj[key]
>result6 : never
>obj[key] : never
>obj : { a: string; b: number; }
>key : never

// Expanded examples from https://github.com/Microsoft/TypeScript/issues/21988
type RequiredPropNames<T> = {
>RequiredPropNames : RequiredPropNames<T>

  [P in keyof T]-?: undefined extends T[P] ? never : P
}[keyof T];

type OptionalPropNames<T> = {
>OptionalPropNames : OptionalPropNames<T>

  [P in keyof T]-?: undefined extends T[P] ? P : never
}[keyof T];

type RequiredProps<T> = { [P in RequiredPropNames<T>]: T[P] };
>RequiredProps : RequiredProps<T>

type OptionalProps<T> = { [P in OptionalPropNames<T>]?: T[P] };
>OptionalProps : OptionalProps<T>

type Match<Exp, Act> = [Exp] extends [Act]
>Match : Match<Exp, Act>

  ? ([Act] extends [Exp] ? "Match" : "Did not match 2")
  : "Did not match 1";

type ExpectType<Exp, Act> = Match<Exp, Act> extends "Match"
>ExpectType : ExpectType<Exp, Act>

  ? ({} extends Exp ? Match<Required<Exp>, Required<Act>> : "Match")
  : "Did not match";

type P3 = { a: string; b: number; c?: boolean };
>P3 : { a: string; b: number; c?: boolean | undefined; }
>a : string
>b : number
>c : boolean | undefined

type P2 = { a: string; c?: boolean };
>P2 : { a: string; c?: boolean | undefined; }
>a : string
>c : boolean | undefined

type P1 = { c?: boolean };
>P1 : { c?: boolean | undefined; }
>c : boolean | undefined

type P0 = {};
>P0 : {}

type P3Names = RequiredPropNames<P3>; // expect 'a' | 'b'
>P3Names : "a" | "b"

type P2Names = RequiredPropNames<P2>; // expect 'a'
>P2Names : "a"

type P1Names = RequiredPropNames<P1>; // expect never
>P1Names : never

type P0Names = RequiredPropNames<P0>; // expect never
>P0Names : never

declare const p3NameTest: ExpectType<"a" | "b", P3Names>;
>p3NameTest : "Match"

declare const p2NameTest: ExpectType<"a", P2Names>;
>p2NameTest : "Match"

declare const p1NameTest: ExpectType<never, P1Names>;
>p1NameTest : "Match"

declare const p0NameTest: ExpectType<never, P0Names>;
>p0NameTest : "Match"

type P3Props = RequiredProps<P3>; // expect { a: string; b: number }
>P3Props : { a: string; b: number; }

type P2Props = RequiredProps<P2>; // expect { a: string; }
>P2Props : { a: string; }

type P1Props = RequiredProps<P1>; // expect {}
>P1Props : {}

type P0Props = RequiredProps<P0>; // expect {}
>P0Props : {}

declare const p3Test: ExpectType<{ a: string; b: number }, P3Props>;
>p3Test : "Match"
>a : string
>b : number

declare const p2Test: ExpectType<{ a: string }, P2Props>;
>p2Test : "Match"
>a : string

declare const p1Test: ExpectType<{}, P1Props>;
>p1Test : "Match"

declare const p0Test: ExpectType<{}, P0Props>;
>p0Test : "Match"

type O3 = { a?: string; b?: number; c: boolean };
>O3 : { a?: string | undefined; b?: number | undefined; c: boolean; }
>a : string | undefined
>b : number | undefined
>c : boolean

type O2 = { a?: string; c: boolean };
>O2 : { a?: string | undefined; c: boolean; }
>a : string | undefined
>c : boolean

type O1 = { c: boolean };
>O1 : { c: boolean; }
>c : boolean

type O0 = {};
>O0 : {}

type O3Names = OptionalPropNames<O3>; // expect 'a' | 'b'
>O3Names : "a" | "b"

type O2Names = OptionalPropNames<O2>; // expect 'a'
>O2Names : "a"

type O1Names = OptionalPropNames<O1>; // expect never
>O1Names : never

type O0Names = OptionalPropNames<O0>; // expect never
>O0Names : never

declare const o3NameTest: ExpectType<"a" | "b", O3Names>;
>o3NameTest : "Match"

declare const o2NameTest: ExpectType<"a", O2Names>;
>o2NameTest : "Match"

declare const o1NameTest: ExpectType<never, O1Names>;
>o1NameTest : "Match"

declare const o0NameTest: ExpectType<never, O0Names>;
>o0NameTest : "Match"

type O3Props = OptionalProps<O3>; // expect { a?: string | undefined; b?: number | undefined }
>O3Props : { a?: string | undefined; b?: number | undefined; }

type O2Props = OptionalProps<O2>; // expect { a?: string | undefined; }
>O2Props : { a?: string | undefined; }

type O1Props = OptionalProps<O1>; // expect {}
>O1Props : {}

type O0Props = OptionalProps<O0>; // expect {}
>O0Props : {}

declare const o3Test: ExpectType<{ a?: string; b?: number }, O3Props>;
>o3Test : "Match"
>a : string | undefined
>b : number | undefined

declare const o2Test: ExpectType<{ a?: string }, O2Props>;
>o2Test : "Match"
>a : string | undefined

declare const o1Test: ExpectType<{}, O1Props>;
>o1Test : "Match"

declare const o0Test: ExpectType<{}, O0Props>;
>o0Test : "Match"

// Repro from #23005

type Example<T extends Record<'a', string>> = T['a'];
>Example : Example<T>

type Res1 = Example<{ a: "x" } | { a: "y" }>;  // "x" | "y"
>Res1 : "x" | "y"
>a : "x"
>a : "y"

type Res2 = Example<{ a: "x" }>;  // "x"
>Res2 : "x"
>a : "x"

type Res3 = Example<never>;  // never 
>Res3 : never