Skip to content

Commit 5ae218d

Browse files
committed
Add test cases for indexedAccessWithFreshObjectLiteral
1 parent e775c2a commit 5ae218d

5 files changed

+513
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
tests/cases/compiler/indexedAccessWithFreshObjectLiteral.ts(34,10): error TS2339: Property 'z' does not exist on type '{ a: number; b: string; c: boolean; }'.
2+
3+
4+
==== tests/cases/compiler/indexedAccessWithFreshObjectLiteral.ts (1 errors) ====
5+
function foo (id: string) {
6+
return {
7+
a: 1,
8+
b: "",
9+
c: true
10+
}[id]
11+
}
12+
13+
function bar (id: 'a' | 'b') {
14+
return {
15+
a: 1,
16+
b: "",
17+
c: false
18+
}[id]
19+
}
20+
21+
function baz (id: '1' | '2') {
22+
return {
23+
1: 1,
24+
2: "",
25+
3: false
26+
}[id]
27+
}
28+
29+
function qux (id: 1 | 2) {
30+
return {
31+
1: 1,
32+
2: "",
33+
3: false
34+
}[id]
35+
}
36+
37+
function quux (id: 'a' | 'b' | 'z') {
38+
return {
39+
~
40+
a: 1,
41+
~~~~~~~~~~~
42+
b: "",
43+
~~~~~~~~~~~~
44+
c: false
45+
~~~~~~~~~~~~~~
46+
}[id]
47+
~~~~~~~
48+
!!! error TS2339: Property 'z' does not exist on type '{ a: number; b: string; c: boolean; }'.
49+
}
50+
51+
function corge(id: string) {
52+
return ({
53+
a: 123,
54+
b: ""
55+
} as Record<string, number | string>)[id]
56+
}
57+
58+
function grault(id: string) {
59+
return ({
60+
a: 123,
61+
b: ""
62+
} as { [k: string]: string | number})[id]
63+
}
64+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//// [indexedAccessWithFreshObjectLiteral.ts]
2+
function foo (id: string) {
3+
return {
4+
a: 1,
5+
b: "",
6+
c: true
7+
}[id]
8+
}
9+
10+
function bar (id: 'a' | 'b') {
11+
return {
12+
a: 1,
13+
b: "",
14+
c: false
15+
}[id]
16+
}
17+
18+
function baz (id: '1' | '2') {
19+
return {
20+
1: 1,
21+
2: "",
22+
3: false
23+
}[id]
24+
}
25+
26+
function qux (id: 1 | 2) {
27+
return {
28+
1: 1,
29+
2: "",
30+
3: false
31+
}[id]
32+
}
33+
34+
function quux (id: 'a' | 'b' | 'z') {
35+
return {
36+
a: 1,
37+
b: "",
38+
c: false
39+
}[id]
40+
}
41+
42+
function corge(id: string) {
43+
return ({
44+
a: 123,
45+
b: ""
46+
} as Record<string, number | string>)[id]
47+
}
48+
49+
function grault(id: string) {
50+
return ({
51+
a: 123,
52+
b: ""
53+
} as { [k: string]: string | number})[id]
54+
}
55+
56+
57+
//// [indexedAccessWithFreshObjectLiteral.js]
58+
"use strict";
59+
function foo(id) {
60+
return {
61+
a: 1,
62+
b: "",
63+
c: true
64+
}[id];
65+
}
66+
function bar(id) {
67+
return {
68+
a: 1,
69+
b: "",
70+
c: false
71+
}[id];
72+
}
73+
function baz(id) {
74+
return {
75+
1: 1,
76+
2: "",
77+
3: false
78+
}[id];
79+
}
80+
function qux(id) {
81+
return {
82+
1: 1,
83+
2: "",
84+
3: false
85+
}[id];
86+
}
87+
function quux(id) {
88+
return {
89+
a: 1,
90+
b: "",
91+
c: false
92+
}[id];
93+
}
94+
function corge(id) {
95+
return {
96+
a: 123,
97+
b: ""
98+
}[id];
99+
}
100+
function grault(id) {
101+
return {
102+
a: 123,
103+
b: ""
104+
}[id];
105+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
=== tests/cases/compiler/indexedAccessWithFreshObjectLiteral.ts ===
2+
function foo (id: string) {
3+
>foo : Symbol(foo, Decl(indexedAccessWithFreshObjectLiteral.ts, 0, 0))
4+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 0, 14))
5+
6+
return {
7+
a: 1,
8+
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 1, 10))
9+
10+
b: "",
11+
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 2, 11))
12+
13+
c: true
14+
>c : Symbol(c, Decl(indexedAccessWithFreshObjectLiteral.ts, 3, 12))
15+
16+
}[id]
17+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 0, 14))
18+
}
19+
20+
function bar (id: 'a' | 'b') {
21+
>bar : Symbol(bar, Decl(indexedAccessWithFreshObjectLiteral.ts, 6, 1))
22+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 8, 14))
23+
24+
return {
25+
a: 1,
26+
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 9, 10))
27+
28+
b: "",
29+
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 10, 11))
30+
31+
c: false
32+
>c : Symbol(c, Decl(indexedAccessWithFreshObjectLiteral.ts, 11, 12))
33+
34+
}[id]
35+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 8, 14))
36+
}
37+
38+
function baz (id: '1' | '2') {
39+
>baz : Symbol(baz, Decl(indexedAccessWithFreshObjectLiteral.ts, 14, 1))
40+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 16, 14))
41+
42+
return {
43+
1: 1,
44+
>1 : Symbol(1, Decl(indexedAccessWithFreshObjectLiteral.ts, 17, 10))
45+
46+
2: "",
47+
>2 : Symbol(2, Decl(indexedAccessWithFreshObjectLiteral.ts, 18, 11))
48+
49+
3: false
50+
>3 : Symbol(3, Decl(indexedAccessWithFreshObjectLiteral.ts, 19, 12))
51+
52+
}[id]
53+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 16, 14))
54+
}
55+
56+
function qux (id: 1 | 2) {
57+
>qux : Symbol(qux, Decl(indexedAccessWithFreshObjectLiteral.ts, 22, 1))
58+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 24, 14))
59+
60+
return {
61+
1: 1,
62+
>1 : Symbol(1, Decl(indexedAccessWithFreshObjectLiteral.ts, 25, 10))
63+
64+
2: "",
65+
>2 : Symbol(2, Decl(indexedAccessWithFreshObjectLiteral.ts, 26, 11))
66+
67+
3: false
68+
>3 : Symbol(3, Decl(indexedAccessWithFreshObjectLiteral.ts, 27, 12))
69+
70+
}[id]
71+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 24, 14))
72+
}
73+
74+
function quux (id: 'a' | 'b' | 'z') {
75+
>quux : Symbol(quux, Decl(indexedAccessWithFreshObjectLiteral.ts, 30, 1))
76+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 32, 15))
77+
78+
return {
79+
a: 1,
80+
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 33, 10))
81+
82+
b: "",
83+
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 34, 11))
84+
85+
c: false
86+
>c : Symbol(c, Decl(indexedAccessWithFreshObjectLiteral.ts, 35, 12))
87+
88+
}[id]
89+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 32, 15))
90+
}
91+
92+
function corge(id: string) {
93+
>corge : Symbol(corge, Decl(indexedAccessWithFreshObjectLiteral.ts, 38, 1))
94+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 40, 15))
95+
96+
return ({
97+
a: 123,
98+
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 41, 11))
99+
100+
b: ""
101+
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 42, 13))
102+
103+
} as Record<string, number | string>)[id]
104+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
105+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 40, 15))
106+
}
107+
108+
function grault(id: string) {
109+
>grault : Symbol(grault, Decl(indexedAccessWithFreshObjectLiteral.ts, 45, 1))
110+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 47, 16))
111+
112+
return ({
113+
a: 123,
114+
>a : Symbol(a, Decl(indexedAccessWithFreshObjectLiteral.ts, 48, 11))
115+
116+
b: ""
117+
>b : Symbol(b, Decl(indexedAccessWithFreshObjectLiteral.ts, 49, 13))
118+
119+
} as { [k: string]: string | number})[id]
120+
>k : Symbol(k, Decl(indexedAccessWithFreshObjectLiteral.ts, 51, 10))
121+
>id : Symbol(id, Decl(indexedAccessWithFreshObjectLiteral.ts, 47, 16))
122+
}
123+

0 commit comments

Comments
 (0)