Skip to content

Commit 37911f8

Browse files
Accepted baselines.
1 parent 54703cd commit 37911f8

File tree

4 files changed

+462
-0
lines changed

4 files changed

+462
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
tests/cases/compiler/narrowingMutualSubtypes.ts(117,17): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
2+
No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
3+
tests/cases/compiler/narrowingMutualSubtypes.ts(118,29): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
4+
No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
5+
6+
7+
==== tests/cases/compiler/narrowingMutualSubtypes.ts (2 errors) ====
8+
// Check that `any` is a strict supertype of `unknown`
9+
10+
declare const ru1: { [x: string]: unknown };
11+
declare const ra1: { [x: string]: any };
12+
13+
const a1a = [ru1, ra1]; // { [x: string]: any }[]
14+
const a1b = [ra1, ru1]; // { [x: string]: any }[]
15+
16+
declare const ra2: { [x: string]: any };
17+
declare const ru2: { [x: string]: unknown };
18+
19+
const a2a = [ru2, ra2]; // { [x: string]: any }[]
20+
const a2b = [ra2, ru2]; // { [x: string]: any }[]
21+
22+
// Check that `{}` is strict supertype of any non-empty object
23+
24+
const c3 = {};
25+
declare const r3: { [x: string]: unknown }
26+
27+
const a3a = [c3, r3]; // {}[]
28+
const a3b = [r3, c3]; // {}[]
29+
30+
declare const r4: { [x: string]: unknown }
31+
const c4 = {};
32+
33+
const a4a = [c4, r4]; // {}[]
34+
const a4b = [r4, c4]; // {}[]
35+
36+
// Check that {} is a strict supertype of Record<string, unknown>
37+
38+
declare function isObject1(value: unknown): value is Record<string, unknown>;
39+
40+
function gg1(x: {}) {
41+
if (isObject1(x)) {
42+
x; // Record<string, unknown>
43+
}
44+
else {
45+
x; // {}
46+
}
47+
x; // {}
48+
}
49+
50+
declare function isObject2(value: unknown): value is {};
51+
52+
function gg2(x: Record<string, unknown>) {
53+
if (isObject2(x)) {
54+
x; // Record<string, unknown>
55+
}
56+
else {
57+
x; // never
58+
}
59+
x; // Record<string, unknown>
60+
}
61+
62+
// Check that {} is a strict supertype of Record<string, any>
63+
64+
declare function isObject3(value: unknown): value is Record<string, any>;
65+
66+
function gg3(x: {}) {
67+
if (isObject3(x)) {
68+
x; // Record<string, any>
69+
}
70+
else {
71+
x; // {}
72+
}
73+
x; // {}
74+
}
75+
76+
declare function isObject4(value: unknown): value is {};
77+
78+
function gg4(x: Record<string, any>) {
79+
if (isObject4(x)) {
80+
x; // Record<string, any>
81+
}
82+
else {
83+
x; // never
84+
}
85+
x; // Record<string, any>
86+
}
87+
88+
// Repro from #50916
89+
90+
type Identity<T> = {[K in keyof T]: T[K]};
91+
92+
type Self<T> = T extends unknown ? Identity<T> : never;
93+
94+
function is<T>(value: T): value is Self<T> {
95+
return true;
96+
}
97+
98+
type Union = {a: number} | {b: number} | {c: number};
99+
100+
function example(x: Union) {
101+
if (is(x)) {}
102+
if (is(x)) {}
103+
if (is(x)) {}
104+
if (is(x)) {}
105+
if (is(x)) {}
106+
if (is(x)) {}
107+
if (is(x)) {}
108+
if (is(x)) {}
109+
x; // Union
110+
}
111+
112+
function checksArrayOrObject1(obj: Record<string, any> | Record<string, any>[]) {
113+
// "accidentally" guards the first branch on the length
114+
if (Array.isArray(obj) && obj.length) {
115+
for (let key in obj) {
116+
if (obj[key] !== undefined) {
117+
console.log(obj[key])
118+
}
119+
}
120+
}
121+
else {
122+
// 'obj' should probably not include an array type here.
123+
for (let key in obj) {
124+
if (obj[key] !== undefined) {
125+
~~~~~~~~
126+
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
127+
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
128+
console.log(obj[key])
129+
~~~~~~~~
130+
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'any[] | Record<string, any>'.
131+
!!! error TS7053: No index signature with a parameter of type 'string' was found on type 'any[] | Record<string, any>'.
132+
}
133+
}
134+
}
135+
}
136+
137+
function checksArrayOrObject2(obj: Record<string, any> | Record<string, any>[]) {
138+
if (Array.isArray(obj)) {
139+
// obj should only be an array type here
140+
for (let key in obj) {
141+
if (obj[key] !== undefined) {
142+
console.log(obj[key])
143+
}
144+
}
145+
}
146+
else {
147+
// 'obj' should probably not include an array type here.
148+
for (let key in obj) {
149+
if (obj[key] !== undefined) {
150+
console.log(obj[key])
151+
}
152+
}
153+
}
154+
}
155+

tests/baselines/reference/narrowingMutualSubtypes.js

+74
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,44 @@ function example(x: Union) {
102102
if (is(x)) {}
103103
x; // Union
104104
}
105+
106+
function checksArrayOrObject1(obj: Record<string, any> | Record<string, any>[]) {
107+
// "accidentally" guards the first branch on the length
108+
if (Array.isArray(obj) && obj.length) {
109+
for (let key in obj) {
110+
if (obj[key] !== undefined) {
111+
console.log(obj[key])
112+
}
113+
}
114+
}
115+
else {
116+
// 'obj' should probably not include an array type here.
117+
for (let key in obj) {
118+
if (obj[key] !== undefined) {
119+
console.log(obj[key])
120+
}
121+
}
122+
}
123+
}
124+
125+
function checksArrayOrObject2(obj: Record<string, any> | Record<string, any>[]) {
126+
if (Array.isArray(obj)) {
127+
// obj should only be an array type here
128+
for (let key in obj) {
129+
if (obj[key] !== undefined) {
130+
console.log(obj[key])
131+
}
132+
}
133+
}
134+
else {
135+
// 'obj' should probably not include an array type here.
136+
for (let key in obj) {
137+
if (obj[key] !== undefined) {
138+
console.log(obj[key])
139+
}
140+
}
141+
}
142+
}
105143

106144

107145
//// [narrowingMutualSubtypes.js]
@@ -168,3 +206,39 @@ function example(x) {
168206
if (is(x)) { }
169207
x; // Union
170208
}
209+
function checksArrayOrObject1(obj) {
210+
// "accidentally" guards the first branch on the length
211+
if (Array.isArray(obj) && obj.length) {
212+
for (var key in obj) {
213+
if (obj[key] !== undefined) {
214+
console.log(obj[key]);
215+
}
216+
}
217+
}
218+
else {
219+
// 'obj' should probably not include an array type here.
220+
for (var key in obj) {
221+
if (obj[key] !== undefined) {
222+
console.log(obj[key]);
223+
}
224+
}
225+
}
226+
}
227+
function checksArrayOrObject2(obj) {
228+
if (Array.isArray(obj)) {
229+
// obj should only be an array type here
230+
for (var key in obj) {
231+
if (obj[key] !== undefined) {
232+
console.log(obj[key]);
233+
}
234+
}
235+
}
236+
else {
237+
// 'obj' should probably not include an array type here.
238+
for (var key in obj) {
239+
if (obj[key] !== undefined) {
240+
console.log(obj[key]);
241+
}
242+
}
243+
}
244+
}

tests/baselines/reference/narrowingMutualSubtypes.symbols

+109
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,112 @@ function example(x: Union) {
253253
>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 92, 17))
254254
}
255255

256+
function checksArrayOrObject1(obj: Record<string, any> | Record<string, any>[]) {
257+
>checksArrayOrObject1 : Symbol(checksArrayOrObject1, Decl(narrowingMutualSubtypes.ts, 102, 1))
258+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
259+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
260+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
261+
262+
// "accidentally" guards the first branch on the length
263+
if (Array.isArray(obj) && obj.length) {
264+
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
265+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
266+
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
267+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
268+
>obj.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
269+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
270+
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
271+
272+
for (let key in obj) {
273+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 107, 16))
274+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
275+
276+
if (obj[key] !== undefined) {
277+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
278+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 107, 16))
279+
>undefined : Symbol(undefined)
280+
281+
console.log(obj[key])
282+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
283+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
284+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
285+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
286+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 107, 16))
287+
}
288+
}
289+
}
290+
else {
291+
// 'obj' should probably not include an array type here.
292+
for (let key in obj) {
293+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 115, 16))
294+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
295+
296+
if (obj[key] !== undefined) {
297+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
298+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 115, 16))
299+
>undefined : Symbol(undefined)
300+
301+
console.log(obj[key])
302+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
303+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
304+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
305+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 104, 30))
306+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 115, 16))
307+
}
308+
}
309+
}
310+
}
311+
312+
function checksArrayOrObject2(obj: Record<string, any> | Record<string, any>[]) {
313+
>checksArrayOrObject2 : Symbol(checksArrayOrObject2, Decl(narrowingMutualSubtypes.ts, 121, 1))
314+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
315+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
316+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
317+
318+
if (Array.isArray(obj)) {
319+
>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
320+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
321+
>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.es5.d.ts, --, --))
322+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
323+
324+
// obj should only be an array type here
325+
for (let key in obj) {
326+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 126, 16))
327+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
328+
329+
if (obj[key] !== undefined) {
330+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
331+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 126, 16))
332+
>undefined : Symbol(undefined)
333+
334+
console.log(obj[key])
335+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
336+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
337+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
338+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
339+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 126, 16))
340+
}
341+
}
342+
}
343+
else {
344+
// 'obj' should probably not include an array type here.
345+
for (let key in obj) {
346+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 134, 16))
347+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
348+
349+
if (obj[key] !== undefined) {
350+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
351+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 134, 16))
352+
>undefined : Symbol(undefined)
353+
354+
console.log(obj[key])
355+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
356+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
357+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
358+
>obj : Symbol(obj, Decl(narrowingMutualSubtypes.ts, 123, 30))
359+
>key : Symbol(key, Decl(narrowingMutualSubtypes.ts, 134, 16))
360+
}
361+
}
362+
}
363+
}
364+

0 commit comments

Comments
 (0)