Skip to content

Commit 5aff92b

Browse files
committed
Update tests and add more tests in a module
1 parent 1cccdf2 commit 5aff92b

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

tests/cases/compiler/narrowingPastLastAssignment.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ function f5(x: string | number, cond: () => boolean) {
6565
action(() => { x /* number */ });
6666
}
6767

68+
function f5a(cond: boolean) {
69+
if (cond) {
70+
let x: number | undefined;
71+
x = 1;
72+
action(() => { x /* number */ });
73+
}
74+
else {
75+
let x: number | undefined;
76+
x = 2;
77+
action(() => { x /* number */ });
78+
}
79+
}
80+
81+
function f5b() {
82+
for (let x = 0; x < 10; x++) {
83+
if (x === 1 || x === 2) {
84+
action(() => { x /* 1 | 2 */ })
85+
}
86+
}
87+
}
88+
6889
// Implicit any variables have a known type following last assignment
6990

7091
function f6() {
@@ -87,6 +108,23 @@ function f7() {
87108
}
88109
}
89110

111+
// Narrowings are not preserved for global variables
112+
113+
let g: string | number;
114+
g = "abc";
115+
action(() => { g /* string | number */ });
116+
117+
// Narrowings are not preserved for exported namespace members
118+
119+
namespace Foo {
120+
export let x: string | number;
121+
x = "abc";
122+
action(() => { x /* string | number */ });
123+
let y: string | number;
124+
y = "abc";
125+
action(() => { y /* string */ });
126+
}
127+
90128
// Repros from #35124
91129

92130
function f10() {
@@ -108,10 +146,12 @@ function f11() {
108146

109147
// Repro from #52104
110148

111-
const fooMap: Map<string,Array<number>> = new Map()
112-
const values = [1, 2, 3, 4, 5];
113-
let foo = fooMap.get("a");
114-
if (foo == null) {
115-
foo = [];
149+
function f12() {
150+
const fooMap: Map<string,Array<number>> = new Map()
151+
const values = [1, 2, 3, 4, 5];
152+
let foo = fooMap.get("a");
153+
if (foo == null) {
154+
foo = [];
155+
}
156+
values.forEach(v => foo.push(v));
116157
}
117-
values.forEach(v => foo.push(v));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @strict: true
2+
// @noEmit: true
3+
// @target: esnext
4+
5+
function action(f: Function) {}
6+
7+
// Narrowings are not preserved for exported mutable variables
8+
9+
export let x1: string | number;
10+
x1 = "abc";
11+
action(() => { x1 /* string | number */ });
12+
13+
export { x2 };
14+
let x2: string | number;
15+
x2 = "abc";
16+
action(() => { x2 /* string | number */ });
17+
18+
export { x3 as foo };
19+
let x3: string | number;
20+
x3 = "abc";
21+
action(() => { x3 /* string | number */ });
22+
23+
let x4: string | number;
24+
x4 = "abc";
25+
action(() => { x4 /* string */ });
26+
export default x4;
27+
28+
let x5: string | number;
29+
x5 = "abc";
30+
action(() => { x5 /* string */ });

0 commit comments

Comments
 (0)