Skip to content

Commit 61104d0

Browse files
committed
Add tests
1 parent 54b981f commit 61104d0

3 files changed

+208
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts ===
2+
declare function invariant(condition: boolean): asserts condition;
3+
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0))
4+
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27))
5+
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27))
6+
7+
function f1(obj: Record<string, string>) {
8+
>f1 : Symbol(f1, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 66))
9+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12))
10+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
11+
12+
invariant("test" in obj);
13+
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0))
14+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12))
15+
16+
return obj.test; // string
17+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12))
18+
}
19+
20+
function f2(obj: Record<string, string>) {
21+
>f2 : Symbol(f2, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 5, 1))
22+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12))
23+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
24+
25+
if ("test" in obj) {
26+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12))
27+
28+
return obj.test; // string
29+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12))
30+
}
31+
return "default";
32+
}
33+
34+
function f3(obj: Record<string, string>) {
35+
>f3 : Symbol(f3, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 12, 1))
36+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12))
37+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
38+
39+
obj.test; // string | undefined
40+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12))
41+
42+
if ("test" in obj) {
43+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12))
44+
45+
obj.test; // string
46+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12))
47+
}
48+
else {
49+
obj.test; // undefined
50+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 14, 12))
51+
}
52+
}
53+
54+
function f4(obj: Record<string, string>) {
55+
>f4 : Symbol(f4, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 22, 1))
56+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12))
57+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
58+
59+
obj.test; // string | undefined
60+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12))
61+
62+
if (obj.hasOwnProperty("test")) {
63+
>obj.hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --))
64+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12))
65+
>hasOwnProperty : Symbol(Object.hasOwnProperty, Decl(lib.es5.d.ts, --, --))
66+
67+
obj.test; // string
68+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12))
69+
}
70+
else {
71+
obj.test; // undefined
72+
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 24, 12))
73+
}
74+
}
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts ===
2+
declare function invariant(condition: boolean): asserts condition;
3+
>invariant : (condition: boolean) => asserts condition
4+
>condition : boolean
5+
6+
function f1(obj: Record<string, string>) {
7+
>f1 : (obj: Record<string, string>) => string
8+
>obj : Record<string, string>
9+
10+
invariant("test" in obj);
11+
>invariant("test" in obj) : void
12+
>invariant : (condition: boolean) => asserts condition
13+
>"test" in obj : boolean
14+
>"test" : "test"
15+
>obj : Record<string, string>
16+
17+
return obj.test; // string
18+
>obj.test : string
19+
>obj : Record<string, string>
20+
>test : string
21+
}
22+
23+
function f2(obj: Record<string, string>) {
24+
>f2 : (obj: Record<string, string>) => string
25+
>obj : Record<string, string>
26+
27+
if ("test" in obj) {
28+
>"test" in obj : boolean
29+
>"test" : "test"
30+
>obj : Record<string, string>
31+
32+
return obj.test; // string
33+
>obj.test : string
34+
>obj : Record<string, string>
35+
>test : string
36+
}
37+
return "default";
38+
>"default" : "default"
39+
}
40+
41+
function f3(obj: Record<string, string>) {
42+
>f3 : (obj: Record<string, string>) => void
43+
>obj : Record<string, string>
44+
45+
obj.test; // string | undefined
46+
>obj.test : string | undefined
47+
>obj : Record<string, string>
48+
>test : string | undefined
49+
50+
if ("test" in obj) {
51+
>"test" in obj : boolean
52+
>"test" : "test"
53+
>obj : Record<string, string>
54+
55+
obj.test; // string
56+
>obj.test : string
57+
>obj : Record<string, string>
58+
>test : string
59+
}
60+
else {
61+
obj.test; // undefined
62+
>obj.test : undefined
63+
>obj : Record<string, string>
64+
>test : undefined
65+
}
66+
}
67+
68+
function f4(obj: Record<string, string>) {
69+
>f4 : (obj: Record<string, string>) => void
70+
>obj : Record<string, string>
71+
72+
obj.test; // string | undefined
73+
>obj.test : string | undefined
74+
>obj : Record<string, string>
75+
>test : string | undefined
76+
77+
if (obj.hasOwnProperty("test")) {
78+
>obj.hasOwnProperty("test") : boolean
79+
>obj.hasOwnProperty : (v: PropertyKey) => boolean
80+
>obj : Record<string, string>
81+
>hasOwnProperty : (v: PropertyKey) => boolean
82+
>"test" : "test"
83+
84+
obj.test; // string
85+
>obj.test : string
86+
>obj : Record<string, string>
87+
>test : string
88+
}
89+
else {
90+
obj.test; // undefined
91+
>obj.test : undefined
92+
>obj : Record<string, string>
93+
>test : undefined
94+
}
95+
}
96+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @strict: true
2+
// @noEmit: true
3+
// @noUncheckedIndexedAccess: true
4+
5+
declare function invariant(condition: boolean): asserts condition;
6+
7+
function f1(obj: Record<string, string>) {
8+
invariant("test" in obj);
9+
return obj.test; // string
10+
}
11+
12+
function f2(obj: Record<string, string>) {
13+
if ("test" in obj) {
14+
return obj.test; // string
15+
}
16+
return "default";
17+
}
18+
19+
function f3(obj: Record<string, string>) {
20+
obj.test; // string | undefined
21+
if ("test" in obj) {
22+
obj.test; // string
23+
}
24+
else {
25+
obj.test; // undefined
26+
}
27+
}
28+
29+
function f4(obj: Record<string, string>) {
30+
obj.test; // string | undefined
31+
if (obj.hasOwnProperty("test")) {
32+
obj.test; // string
33+
}
34+
else {
35+
obj.test; // undefined
36+
}
37+
}

0 commit comments

Comments
 (0)