Skip to content

Commit 0b0b68e

Browse files
authored
Merge pull request #12312 from Microsoft/widen-literal-types-of-parameter-properties
Widen literal types of parameter properties
2 parents 6f409b5 + 5dbe5f4 commit 0b0b68e

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14744,7 +14744,7 @@ namespace ts {
1474414744
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) {
1474514745
const type = checkExpressionCached(declaration.initializer);
1474614746
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
14747-
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly ||
14747+
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
1474814748
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
1474914749
}
1475014750

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4497,7 +4497,7 @@ namespace ts {
44974497
}
44984498
}
44994499

4500-
export function isParameterPropertyDeclaration(node: ParameterDeclaration): boolean {
4500+
export function isParameterPropertyDeclaration(node: Node): boolean {
45014501
return hasModifier(node, ModifierFlags.ParameterPropertyModifier) && node.parent.kind === SyntaxKind.Constructor && isClassLike(node.parent.parent);
45024502
}
45034503

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/conformance/types/literal/literalTypesWidenInParameterPosition.ts(4,9): error TS2322: Type '5' is not assignable to type '1'.
2+
3+
4+
==== tests/cases/conformance/types/literal/literalTypesWidenInParameterPosition.ts (1 errors) ====
5+
class D {
6+
readonly noWiden = 1
7+
constructor(readonly widen = 2) {
8+
this.noWiden = 5; // error
9+
~~~~~~~~~~~~
10+
!!! error TS2322: Type '5' is not assignable to type '1'.
11+
this.widen = 6; // ok
12+
}
13+
}
14+
new D(7); // ok
15+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [literalTypesWidenInParameterPosition.ts]
2+
class D {
3+
readonly noWiden = 1
4+
constructor(readonly widen = 2) {
5+
this.noWiden = 5; // error
6+
this.widen = 6; // ok
7+
}
8+
}
9+
new D(7); // ok
10+
11+
12+
//// [literalTypesWidenInParameterPosition.js]
13+
var D = (function () {
14+
function D(widen) {
15+
if (widen === void 0) { widen = 2; }
16+
this.widen = widen;
17+
this.noWiden = 1;
18+
this.noWiden = 5; // error
19+
this.widen = 6; // ok
20+
}
21+
return D;
22+
}());
23+
new D(7); // ok
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class D {
2+
readonly noWiden = 1
3+
constructor(readonly widen = 2) {
4+
this.noWiden = 5; // error
5+
this.widen = 6; // ok
6+
}
7+
}
8+
new D(7); // ok

0 commit comments

Comments
 (0)