Skip to content

Commit d543028

Browse files
committed
Add test case for narrowed const in object literal and class expression method
1 parent d34916a commit d543028

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/narrowedConstInMethod.ts(6,28): error TS2531: Object is possibly 'null'.
2+
tests/cases/compiler/narrowedConstInMethod.ts(15,28): error TS2531: Object is possibly 'null'.
3+
4+
5+
==== tests/cases/compiler/narrowedConstInMethod.ts (2 errors) ====
6+
7+
function f() {
8+
const x: string | null = <any>{};
9+
if (x !== null) {
10+
return {
11+
bar() { return x.length; } // Error: possibly null x
12+
~
13+
!!! error TS2531: Object is possibly 'null'.
14+
};
15+
}
16+
}
17+
18+
function f2() {
19+
const x: string | null = <any>{};
20+
if (x !== null) {
21+
return class {
22+
bar() { return x.length; } // Error: possibly null x
23+
~
24+
!!! error TS2531: Object is possibly 'null'.
25+
};
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [narrowedConstInMethod.ts]
2+
3+
function f() {
4+
const x: string | null = <any>{};
5+
if (x !== null) {
6+
return {
7+
bar() { return x.length; } // Error: possibly null x
8+
};
9+
}
10+
}
11+
12+
function f2() {
13+
const x: string | null = <any>{};
14+
if (x !== null) {
15+
return class {
16+
bar() { return x.length; } // Error: possibly null x
17+
};
18+
}
19+
}
20+
21+
//// [narrowedConstInMethod.js]
22+
function f() {
23+
var x = {};
24+
if (x !== null) {
25+
return {
26+
bar: function () { return x.length; } // Error: possibly null x
27+
};
28+
}
29+
}
30+
function f2() {
31+
var x = {};
32+
if (x !== null) {
33+
return (function () {
34+
function class_1() {
35+
}
36+
class_1.prototype.bar = function () { return x.length; }; // Error: possibly null x
37+
return class_1;
38+
}());
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @strictNullChecks: true
2+
3+
function f() {
4+
const x: string | null = <any>{};
5+
if (x !== null) {
6+
return {
7+
bar() { return x.length; } // Error: possibly null x
8+
};
9+
}
10+
}
11+
12+
function f2() {
13+
const x: string | null = <any>{};
14+
if (x !== null) {
15+
return class {
16+
bar() { return x.length; } // Error: possibly null x
17+
};
18+
}
19+
}

0 commit comments

Comments
 (0)