Skip to content

Commit b9b07c4

Browse files
committed
Accept new baselines
1 parent b93a10b commit b9b07c4

File tree

4 files changed

+606
-183
lines changed

4 files changed

+606
-183
lines changed

tests/baselines/reference/controlFlowGenericTypes.errors.txt

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist o
55
controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
66
controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'.
77
Property 'foo' does not exist on type 'AA'.
8-
controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'.
9-
controlFlowGenericTypes.ts(167,9): error TS18048: 'iSpec' is possibly 'undefined'.
10-
controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined'.
8+
controlFlowGenericTypes.ts(176,16): error TS18049: 'obj' is possibly 'null' or 'undefined'.
9+
controlFlowGenericTypes.ts(182,16): error TS18049: 'obj' is possibly 'null' or 'undefined'.
10+
controlFlowGenericTypes.ts(195,9): error TS2536: Type 'keyof PublicSpec' cannot be used to index type 'InternalSpec'.
1111

1212

1313
==== controlFlowGenericTypes.ts (8 errors) ====
@@ -163,25 +163,54 @@ controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined
163163
emittingObject.off(eventName as typeof eventName, 0);
164164
}
165165

166-
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
167-
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
168-
// and x are of generic types T and K, we want the resulting type to be T[K].
166+
// In an element access obj[key], we consider obj to be in a constraint position, except when
167+
// obj and key both have generic types. When obj and key are of generic types T and K, we want
168+
// the resulting type to be T[K].
169169

170170
function fx1<T, K extends keyof T>(obj: T, key: K) {
171171
const x1 = obj[key];
172172
const x2 = obj && obj[key];
173+
const x3 = obj?.[key];
173174
}
174175

175176
function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
176177
const x1 = obj[key];
177178
const x2 = obj && obj[key];
179+
const x3 = obj?.[key];
178180
}
179181

180182
function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
183+
const x1 = obj[key];
184+
const x2 = obj && obj[key];
185+
const x3 = obj?.[key];
186+
}
187+
188+
function fx4<T extends unknown, K extends keyof T>(obj: T, key: K) {
189+
const x1 = obj[key];
190+
const x2 = obj && obj[key];
191+
const x3 = obj?.[key];
192+
}
193+
194+
function fx5<T extends {} | null | undefined, K extends keyof T>(obj: T, key: K) {
195+
const x1 = obj[key];
196+
const x2 = obj && obj[key];
197+
const x3 = obj?.[key];
198+
}
199+
200+
function fx6<T, K extends keyof T>(obj: T | null | undefined, key: K) {
181201
const x1 = obj[key]; // Error
182202
~~~
183-
!!! error TS18048: 'obj' is possibly 'undefined'.
203+
!!! error TS18049: 'obj' is possibly 'null' or 'undefined'.
184204
const x2 = obj && obj[key];
205+
const x3 = obj?.[key];
206+
}
207+
208+
function fx7<T, K extends keyof T>(obj: { x: T } | null | undefined, key: K) {
209+
const x1 = obj.x[key]; // Error
210+
~~~
211+
!!! error TS18049: 'obj' is possibly 'null' or 'undefined'.
212+
const x2 = obj && obj.x[key];
213+
const x3 = obj?.x[key];
185214
}
186215

187216
// Repro from #44166
@@ -191,12 +220,10 @@ controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined
191220
InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
192221
m() {
193222
let iSpec = null! as InternalSpec;
194-
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
195-
~~~~~
196-
!!! error TS18048: 'iSpec' is possibly 'undefined'.
197-
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
198-
~~~~~
199-
!!! error TS18048: 'iSpec' is possibly 'undefined'.
223+
iSpec[null! as keyof InternalSpec];
224+
iSpec[null! as keyof PublicSpec]; // Error
225+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226+
!!! error TS2536: Type 'keyof PublicSpec' cannot be used to index type 'InternalSpec'.
200227
if (iSpec === undefined) {
201228
return;
202229
}

tests/baselines/reference/controlFlowGenericTypes.js

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,50 @@ function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyo
141141
emittingObject.off(eventName as typeof eventName, 0);
142142
}
143143

144-
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
145-
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
146-
// and x are of generic types T and K, we want the resulting type to be T[K].
144+
// In an element access obj[key], we consider obj to be in a constraint position, except when
145+
// obj and key both have generic types. When obj and key are of generic types T and K, we want
146+
// the resulting type to be T[K].
147147

148148
function fx1<T, K extends keyof T>(obj: T, key: K) {
149149
const x1 = obj[key];
150150
const x2 = obj && obj[key];
151+
const x3 = obj?.[key];
151152
}
152153

153154
function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
154155
const x1 = obj[key];
155156
const x2 = obj && obj[key];
157+
const x3 = obj?.[key];
156158
}
157159

158160
function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
161+
const x1 = obj[key];
162+
const x2 = obj && obj[key];
163+
const x3 = obj?.[key];
164+
}
165+
166+
function fx4<T extends unknown, K extends keyof T>(obj: T, key: K) {
167+
const x1 = obj[key];
168+
const x2 = obj && obj[key];
169+
const x3 = obj?.[key];
170+
}
171+
172+
function fx5<T extends {} | null | undefined, K extends keyof T>(obj: T, key: K) {
173+
const x1 = obj[key];
174+
const x2 = obj && obj[key];
175+
const x3 = obj?.[key];
176+
}
177+
178+
function fx6<T, K extends keyof T>(obj: T | null | undefined, key: K) {
159179
const x1 = obj[key]; // Error
160180
const x2 = obj && obj[key];
181+
const x3 = obj?.[key];
182+
}
183+
184+
function fx7<T, K extends keyof T>(obj: { x: T } | null | undefined, key: K) {
185+
const x1 = obj.x[key]; // Error
186+
const x2 = obj && obj.x[key];
187+
const x3 = obj?.x[key];
161188
}
162189

163190
// Repro from #44166
@@ -167,8 +194,8 @@ class TableBaseEnum<
167194
InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
168195
m() {
169196
let iSpec = null! as InternalSpec;
170-
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
171-
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
197+
iSpec[null! as keyof InternalSpec];
198+
iSpec[null! as keyof PublicSpec]; // Error
172199
if (iSpec === undefined) {
173200
return;
174201
}
@@ -327,29 +354,52 @@ function once(emittingObject, eventName) {
327354
emittingObject.off(eventName, 0);
328355
emittingObject.off(eventName, 0);
329356
}
330-
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
331-
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
332-
// and x are of generic types T and K, we want the resulting type to be T[K].
357+
// In an element access obj[key], we consider obj to be in a constraint position, except when
358+
// obj and key both have generic types. When obj and key are of generic types T and K, we want
359+
// the resulting type to be T[K].
333360
function fx1(obj, key) {
334361
var x1 = obj[key];
335362
var x2 = obj && obj[key];
363+
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
336364
}
337365
function fx2(obj, key) {
338366
var x1 = obj[key];
339367
var x2 = obj && obj[key];
368+
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
340369
}
341370
function fx3(obj, key) {
371+
var x1 = obj[key];
372+
var x2 = obj && obj[key];
373+
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
374+
}
375+
function fx4(obj, key) {
376+
var x1 = obj[key];
377+
var x2 = obj && obj[key];
378+
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
379+
}
380+
function fx5(obj, key) {
381+
var x1 = obj[key];
382+
var x2 = obj && obj[key];
383+
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
384+
}
385+
function fx6(obj, key) {
342386
var x1 = obj[key]; // Error
343387
var x2 = obj && obj[key];
388+
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
389+
}
390+
function fx7(obj, key) {
391+
var x1 = obj.x[key]; // Error
392+
var x2 = obj && obj.x[key];
393+
var x3 = obj === null || obj === void 0 ? void 0 : obj.x[key];
344394
}
345395
// Repro from #44166
346396
var TableBaseEnum = /** @class */ (function () {
347397
function TableBaseEnum() {
348398
}
349399
TableBaseEnum.prototype.m = function () {
350400
var iSpec = null;
351-
iSpec[null]; // Error, object possibly undefined
352-
iSpec[null]; // Error, object possibly undefined
401+
iSpec[null];
402+
iSpec[null]; // Error
353403
if (iSpec === undefined) {
354404
return;
355405
}

0 commit comments

Comments
 (0)