Skip to content

Commit da01975

Browse files
committed
Merge pull request #8295 from Microsoft/anyDefaultsToAny
Variable of type any has initial type any in control flow analysis
2 parents 71fc581 + 6cd7db8 commit da01975

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7972,7 +7972,7 @@ namespace ts {
79727972
return type;
79737973
}
79747974
const declaration = localOrExportSymbol.valueDeclaration;
7975-
const defaultsToDeclaredType = !strictNullChecks || !declaration ||
7975+
const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration ||
79767976
declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) ||
79777977
getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node);
79787978
if (defaultsToDeclaredType && !(type.flags & TypeFlags.Narrowable)) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [defaultOfAnyInStrictNullChecks.ts]
2+
3+
// Regression test for #8295
4+
5+
function foo() {
6+
try {
7+
}
8+
catch (e) {
9+
let s = e.message;
10+
}
11+
}
12+
13+
14+
//// [defaultOfAnyInStrictNullChecks.js]
15+
// Regression test for #8295
16+
function foo() {
17+
try {
18+
}
19+
catch (e) {
20+
var s = e.message;
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/defaultOfAnyInStrictNullChecks.ts ===
2+
3+
// Regression test for #8295
4+
5+
function foo() {
6+
>foo : Symbol(foo, Decl(defaultOfAnyInStrictNullChecks.ts, 0, 0))
7+
8+
try {
9+
}
10+
catch (e) {
11+
>e : Symbol(e, Decl(defaultOfAnyInStrictNullChecks.ts, 6, 11))
12+
13+
let s = e.message;
14+
>s : Symbol(s, Decl(defaultOfAnyInStrictNullChecks.ts, 7, 11))
15+
>e : Symbol(e, Decl(defaultOfAnyInStrictNullChecks.ts, 6, 11))
16+
}
17+
}
18+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/defaultOfAnyInStrictNullChecks.ts ===
2+
3+
// Regression test for #8295
4+
5+
function foo() {
6+
>foo : () => void
7+
8+
try {
9+
}
10+
catch (e) {
11+
>e : any
12+
13+
let s = e.message;
14+
>s : any
15+
>e.message : any
16+
>e : any
17+
>message : any
18+
}
19+
}
20+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @strictNullChecks: true
2+
3+
// Regression test for #8295
4+
5+
function foo() {
6+
try {
7+
}
8+
catch (e) {
9+
let s = e.message;
10+
}
11+
}

0 commit comments

Comments
 (0)