Skip to content

Commit 85815b0

Browse files
committed
Add tests
1 parent 1274ab1 commit 85815b0

4 files changed

+265
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [inferenceOptionalPropertiesToIndexSignatures.ts]
2+
declare function foo<T>(obj: { [x: string]: T }): T;
3+
4+
declare const x1: { a: string, b: number };
5+
declare const x2: { a: string, b: number | undefined };
6+
declare const x3: { a: string, b?: number };
7+
declare const x4: { a: string, b?: number | undefined };
8+
9+
let a1 = foo(x1); // string | number
10+
let a2 = foo(x2); // string | number | undefined
11+
let a3 = foo(x3); // string | number
12+
let a4 = foo(x4); // string | number
13+
14+
// Repro from #43045
15+
16+
const param2 = Math.random() < 0.5 ? 'value2' : null;
17+
18+
const obj = {
19+
param1: 'value1',
20+
...(param2 ? {param2} : {})
21+
};
22+
23+
const query = Object.entries(obj).map(
24+
([k, v]) => `${k}=${encodeURIComponent(v)}`
25+
).join('&');
26+
27+
28+
//// [inferenceOptionalPropertiesToIndexSignatures.js]
29+
"use strict";
30+
let a1 = foo(x1); // string | number
31+
let a2 = foo(x2); // string | number | undefined
32+
let a3 = foo(x3); // string | number
33+
let a4 = foo(x4); // string | number
34+
// Repro from #43045
35+
const param2 = Math.random() < 0.5 ? 'value2' : null;
36+
const obj = {
37+
param1: 'value1',
38+
...(param2 ? { param2 } : {})
39+
};
40+
const query = Object.entries(obj).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=== tests/cases/compiler/inferenceOptionalPropertiesToIndexSignatures.ts ===
2+
declare function foo<T>(obj: { [x: string]: T }): T;
3+
>foo : Symbol(foo, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 0))
4+
>T : Symbol(T, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 21))
5+
>obj : Symbol(obj, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 24))
6+
>x : Symbol(x, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 32))
7+
>T : Symbol(T, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 21))
8+
>T : Symbol(T, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 21))
9+
10+
declare const x1: { a: string, b: number };
11+
>x1 : Symbol(x1, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 2, 13))
12+
>a : Symbol(a, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 2, 19))
13+
>b : Symbol(b, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 2, 30))
14+
15+
declare const x2: { a: string, b: number | undefined };
16+
>x2 : Symbol(x2, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 3, 13))
17+
>a : Symbol(a, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 3, 19))
18+
>b : Symbol(b, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 3, 30))
19+
20+
declare const x3: { a: string, b?: number };
21+
>x3 : Symbol(x3, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 4, 13))
22+
>a : Symbol(a, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 4, 19))
23+
>b : Symbol(b, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 4, 30))
24+
25+
declare const x4: { a: string, b?: number | undefined };
26+
>x4 : Symbol(x4, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 5, 13))
27+
>a : Symbol(a, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 5, 19))
28+
>b : Symbol(b, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 5, 30))
29+
30+
let a1 = foo(x1); // string | number
31+
>a1 : Symbol(a1, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 7, 3))
32+
>foo : Symbol(foo, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 0))
33+
>x1 : Symbol(x1, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 2, 13))
34+
35+
let a2 = foo(x2); // string | number | undefined
36+
>a2 : Symbol(a2, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 8, 3))
37+
>foo : Symbol(foo, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 0))
38+
>x2 : Symbol(x2, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 3, 13))
39+
40+
let a3 = foo(x3); // string | number
41+
>a3 : Symbol(a3, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 9, 3))
42+
>foo : Symbol(foo, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 0))
43+
>x3 : Symbol(x3, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 4, 13))
44+
45+
let a4 = foo(x4); // string | number
46+
>a4 : Symbol(a4, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 10, 3))
47+
>foo : Symbol(foo, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 0, 0))
48+
>x4 : Symbol(x4, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 5, 13))
49+
50+
// Repro from #43045
51+
52+
const param2 = Math.random() < 0.5 ? 'value2' : null;
53+
>param2 : Symbol(param2, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 14, 5))
54+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
55+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
56+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
57+
58+
const obj = {
59+
>obj : Symbol(obj, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 16, 5))
60+
61+
param1: 'value1',
62+
>param1 : Symbol(param1, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 16, 13))
63+
64+
...(param2 ? {param2} : {})
65+
>param2 : Symbol(param2, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 14, 5))
66+
>param2 : Symbol(param2, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 18, 18))
67+
68+
};
69+
70+
const query = Object.entries(obj).map(
71+
>query : Symbol(query, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 21, 5))
72+
>Object.entries(obj).map( ([k, v]) => `${k}=${encodeURIComponent(v)}`).join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
73+
>Object.entries(obj).map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
74+
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
75+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
76+
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
77+
>obj : Symbol(obj, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 16, 5))
78+
>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --))
79+
80+
([k, v]) => `${k}=${encodeURIComponent(v)}`
81+
>k : Symbol(k, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 22, 6))
82+
>v : Symbol(v, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 22, 8))
83+
>k : Symbol(k, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 22, 6))
84+
>encodeURIComponent : Symbol(encodeURIComponent, Decl(lib.es5.d.ts, --, --))
85+
>v : Symbol(v, Decl(inferenceOptionalPropertiesToIndexSignatures.ts, 22, 8))
86+
87+
).join('&');
88+
>join : Symbol(Array.join, Decl(lib.es5.d.ts, --, --))
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
=== tests/cases/compiler/inferenceOptionalPropertiesToIndexSignatures.ts ===
2+
declare function foo<T>(obj: { [x: string]: T }): T;
3+
>foo : <T>(obj: { [x: string]: T; }) => T
4+
>obj : { [x: string]: T; }
5+
>x : string
6+
7+
declare const x1: { a: string, b: number };
8+
>x1 : { a: string; b: number; }
9+
>a : string
10+
>b : number
11+
12+
declare const x2: { a: string, b: number | undefined };
13+
>x2 : { a: string; b: number | undefined; }
14+
>a : string
15+
>b : number | undefined
16+
17+
declare const x3: { a: string, b?: number };
18+
>x3 : { a: string; b?: number | undefined; }
19+
>a : string
20+
>b : number | undefined
21+
22+
declare const x4: { a: string, b?: number | undefined };
23+
>x4 : { a: string; b?: number | undefined; }
24+
>a : string
25+
>b : number | undefined
26+
27+
let a1 = foo(x1); // string | number
28+
>a1 : string | number
29+
>foo(x1) : string | number
30+
>foo : <T>(obj: { [x: string]: T; }) => T
31+
>x1 : { a: string; b: number; }
32+
33+
let a2 = foo(x2); // string | number | undefined
34+
>a2 : string | number | undefined
35+
>foo(x2) : string | number | undefined
36+
>foo : <T>(obj: { [x: string]: T; }) => T
37+
>x2 : { a: string; b: number | undefined; }
38+
39+
let a3 = foo(x3); // string | number
40+
>a3 : string | number
41+
>foo(x3) : string | number
42+
>foo : <T>(obj: { [x: string]: T; }) => T
43+
>x3 : { a: string; b?: number | undefined; }
44+
45+
let a4 = foo(x4); // string | number
46+
>a4 : string | number
47+
>foo(x4) : string | number
48+
>foo : <T>(obj: { [x: string]: T; }) => T
49+
>x4 : { a: string; b?: number | undefined; }
50+
51+
// Repro from #43045
52+
53+
const param2 = Math.random() < 0.5 ? 'value2' : null;
54+
>param2 : "value2" | null
55+
>Math.random() < 0.5 ? 'value2' : null : "value2" | null
56+
>Math.random() < 0.5 : boolean
57+
>Math.random() : number
58+
>Math.random : () => number
59+
>Math : Math
60+
>random : () => number
61+
>0.5 : 0.5
62+
>'value2' : "value2"
63+
>null : null
64+
65+
const obj = {
66+
>obj : { param2?: string | undefined; param1: string; }
67+
>{ param1: 'value1', ...(param2 ? {param2} : {})} : { param2?: string | undefined; param1: string; }
68+
69+
param1: 'value1',
70+
>param1 : string
71+
>'value1' : "value1"
72+
73+
...(param2 ? {param2} : {})
74+
>(param2 ? {param2} : {}) : { param2: string; } | {}
75+
>param2 ? {param2} : {} : { param2: string; } | {}
76+
>param2 : "value2" | null
77+
>{param2} : { param2: string; }
78+
>param2 : string
79+
>{} : {}
80+
81+
};
82+
83+
const query = Object.entries(obj).map(
84+
>query : string
85+
>Object.entries(obj).map( ([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&') : string
86+
>Object.entries(obj).map( ([k, v]) => `${k}=${encodeURIComponent(v)}`).join : (separator?: string | undefined) => string
87+
>Object.entries(obj).map( ([k, v]) => `${k}=${encodeURIComponent(v)}`) : string[]
88+
>Object.entries(obj).map : <U>(callbackfn: (value: [string, string], index: number, array: [string, string][]) => U, thisArg?: any) => U[]
89+
>Object.entries(obj) : [string, string][]
90+
>Object.entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
91+
>Object : ObjectConstructor
92+
>entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
93+
>obj : { param2?: string | undefined; param1: string; }
94+
>map : <U>(callbackfn: (value: [string, string], index: number, array: [string, string][]) => U, thisArg?: any) => U[]
95+
96+
([k, v]) => `${k}=${encodeURIComponent(v)}`
97+
>([k, v]) => `${k}=${encodeURIComponent(v)}` : ([k, v]: [string, string]) => string
98+
>k : string
99+
>v : string
100+
>`${k}=${encodeURIComponent(v)}` : string
101+
>k : string
102+
>encodeURIComponent(v) : string
103+
>encodeURIComponent : (uriComponent: string | number | boolean) => string
104+
>v : string
105+
106+
).join('&');
107+
>join : (separator?: string | undefined) => string
108+
>'&' : "&"
109+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @strict: true
2+
// @target: esnext
3+
4+
declare function foo<T>(obj: { [x: string]: T }): T;
5+
6+
declare const x1: { a: string, b: number };
7+
declare const x2: { a: string, b: number | undefined };
8+
declare const x3: { a: string, b?: number };
9+
declare const x4: { a: string, b?: number | undefined };
10+
11+
let a1 = foo(x1); // string | number
12+
let a2 = foo(x2); // string | number | undefined
13+
let a3 = foo(x3); // string | number
14+
let a4 = foo(x4); // string | number
15+
16+
// Repro from #43045
17+
18+
const param2 = Math.random() < 0.5 ? 'value2' : null;
19+
20+
const obj = {
21+
param1: 'value1',
22+
...(param2 ? {param2} : {})
23+
};
24+
25+
const query = Object.entries(obj).map(
26+
([k, v]) => `${k}=${encodeURIComponent(v)}`
27+
).join('&');

0 commit comments

Comments
 (0)