Skip to content

Commit 24ddbe4

Browse files
committed
Widen after sub-type-reduction took place
1 parent 914150f commit 24ddbe4

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,7 @@ namespace ts {
33783378
}
33793379

33803380
// Return the inferred type for a variable, parameter, or property declaration
3381-
function getWidenedTypeForJSSpecialPropertyDeclaration(declaration: Declaration): Type {
3381+
function getTypeForJSSpecialPropertyDeclaration(declaration: Declaration): Type {
33823382
const expression = declaration.kind === SyntaxKind.BinaryExpression ? <BinaryExpression>declaration :
33833383
declaration.kind === SyntaxKind.PropertyAccessExpression ? <BinaryExpression>getAncestor(declaration, SyntaxKind.BinaryExpression) :
33843384
undefined;
@@ -3395,7 +3395,7 @@ namespace ts {
33953395
}
33963396
}
33973397

3398-
return getWidenedType(getWidenedLiteralType(checkExpressionCached(expression.right)));
3398+
return getWidenedLiteralType(checkExpressionCached(expression.right));
33993399
}
34003400

34013401
// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
@@ -3552,7 +3552,7 @@ namespace ts {
35523552
// * className.prototype.method = expr
35533553
if (declaration.kind === SyntaxKind.BinaryExpression ||
35543554
declaration.kind === SyntaxKind.PropertyAccessExpression && declaration.parent.kind === SyntaxKind.BinaryExpression) {
3555-
type = getUnionType(map(symbol.declarations, getWidenedTypeForJSSpecialPropertyDeclaration), /*subtypeReduction*/ true);
3555+
type = getWidenedType(getUnionType(map(symbol.declarations, getTypeForJSSpecialPropertyDeclaration), /*subtypeReduction*/ true));
35563556
}
35573557
else {
35583558
type = getWidenedTypeForVariableLikeDeclaration(<VariableLikeDeclaration>declaration, /*reportErrors*/ true);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/bar.ts(2,1): error TS2322: Type '"string"' is not assignable to type 'number'.
2+
3+
4+
==== tests/cases/compiler/foo.js (0 errors) ====
5+
6+
class C {
7+
constructor() {
8+
if (cond) {
9+
this.p = null;
10+
}
11+
else {
12+
this.p = 0;
13+
}
14+
}
15+
}
16+
17+
==== tests/cases/compiler/bar.ts (1 errors) ====
18+
19+
(new C()).p = "string"; // Error
20+
~~~~~~~~~~~
21+
!!! error TS2322: Type '"string"' is not assignable to type 'number'.
22+

tests/baselines/reference/multipleDeclarations.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ C.prototype.m = function() {
2121
this.nothing();
2222
>this.nothing() : any
2323
>this.nothing : any
24-
>this : { m: any; }
24+
>this : { m: () => void; }
2525
>nothing : any
2626
}
2727
class X {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
4+
// @filename: foo.js
5+
class C {
6+
constructor() {
7+
if (cond) {
8+
this.p = null;
9+
}
10+
else {
11+
this.p = 0;
12+
}
13+
}
14+
}
15+
16+
// @filename: bar.ts
17+
18+
(new C()).p = "string"; // Error

0 commit comments

Comments
 (0)