@@ -13268,6 +13268,11 @@ namespace ts {
13268
13268
isUnitType(type);
13269
13269
}
13270
13270
13271
+ function isStringOrNumericLiteralType(type: Type): boolean {
13272
+ return type.flags & TypeFlags.Union ? every((<UnionType>type).types, t => !!(t.flags & TypeFlags.StringOrNumberLiteral && !(t.flags & (TypeFlags.EnumLike | TypeFlags.Intrinsic)))) :
13273
+ !!(type.flags & TypeFlags.StringOrNumberLiteral) && !(type.flags & (TypeFlags.EnumLike | TypeFlags.Intrinsic))
13274
+ }
13275
+
13271
13276
function getBaseTypeOfLiteralType(type: Type): Type {
13272
13277
return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(<LiteralType>type) :
13273
13278
type.flags & TypeFlags.StringLiteral ? stringType :
@@ -21803,17 +21808,7 @@ namespace ts {
21803
21808
}
21804
21809
21805
21810
function checkPostfixUnaryExpression(node: PostfixUnaryExpression): Type {
21806
- let operandType: Type;
21807
- const symbol = isEntityNameExpression(node.operand) ? isPropertyAccessEntityNameExpression(node.operand) ?
21808
- getSymbolAtLocation((<PropertyAccessExpression>node.operand).name) : getResolvedSymbol(node.operand as Identifier) : undefined;
21809
- if (symbol && !(getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Const)) {
21810
- operandType = getTypeOfSymbol(symbol);
21811
- if (isLiteralType(operandType) && !(operandType.flags & (TypeFlags.EnumLike | TypeFlags.BooleanLike | TypeFlags.Undefined))) {
21812
- error(node.operand, Diagnostics.The_literal_type_0_cannot_be_modified, typeToString(operandType));
21813
- }
21814
- }
21815
- operandType = checkExpression(node.operand);
21816
-
21811
+ const operandType = checkExpression(node.operand);
21817
21812
if (operandType === silentNeverType) {
21818
21813
return silentNeverType;
21819
21814
}
@@ -21838,6 +21833,21 @@ namespace ts {
21838
21833
return numberType;
21839
21834
}
21840
21835
21836
+ function checkUnaryExpression(node: PrefixUnaryExpression | PostfixUnaryExpression): Type {
21837
+ const symbol = isEntityNameExpression(node.operand) ? isPropertyAccessEntityNameExpression(node.operand) ?
21838
+ getSymbolAtLocation((<PropertyAccessExpression>node.operand).name) : getResolvedSymbol(node.operand as Identifier) : undefined;
21839
+ if (symbol && isUnaryAssignmentOperator(node.operator) && !(getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Const)) {
21840
+ const operandType = getTypeOfSymbol(symbol);
21841
+ if (isStringOrNumericLiteralType(operandType)) {
21842
+ error(node.operand, Diagnostics.The_literal_type_0_cannot_be_modified, typeToString(operandType));
21843
+ }
21844
+ }
21845
+ if (node.kind == SyntaxKind.PrefixUnaryExpression) {
21846
+ return checkPrefixUnaryExpression(node)
21847
+ }
21848
+ return checkPostfixUnaryExpression(node)
21849
+ }
21850
+
21841
21851
// Return true if type might be of the given kind. A union or intersection type might be of a given
21842
21852
// kind if at least one constituent type is of the given kind.
21843
21853
function maybeTypeOfKind(type: Type, kind: TypeFlags): boolean {
@@ -22186,7 +22196,7 @@ namespace ts {
22186
22196
getSymbolAtLocation((<PropertyAccessExpression>left).name) : getResolvedSymbol(left as Identifier) : undefined;
22187
22197
if (symbol && isCompoundAssignmentOperator(operator) && !(getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Const)) {
22188
22198
leftType = getTypeOfSymbol(getResolvedSymbol(left as Identifier));
22189
- if (isLiteralType (leftType) && !(leftType.flags & (TypeFlags.EnumLike | TypeFlags.BooleanLike | TypeFlags.Undefined) )) {
22199
+ if (isStringOrNumericLiteralType (leftType)) {
22190
22200
error(left, Diagnostics.The_literal_type_0_cannot_be_modified, typeToString(leftType));
22191
22201
}
22192
22202
}
@@ -22885,9 +22895,8 @@ namespace ts {
22885
22895
case SyntaxKind.AwaitExpression:
22886
22896
return checkAwaitExpression(<AwaitExpression>node);
22887
22897
case SyntaxKind.PrefixUnaryExpression:
22888
- return checkPrefixUnaryExpression(<PrefixUnaryExpression>node);
22889
22898
case SyntaxKind.PostfixUnaryExpression:
22890
- return checkPostfixUnaryExpression(< PostfixUnaryExpression>node);
22899
+ return checkUnaryExpression(<PrefixUnaryExpression | PostfixUnaryExpression>node);
22891
22900
case SyntaxKind.BinaryExpression:
22892
22901
return checkBinaryExpression(<BinaryExpression>node, checkMode);
22893
22902
case SyntaxKind.ConditionalExpression:
0 commit comments