mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
Support nullable types with expression operators
This commit is contained in:
parent
fa36ff85ca
commit
0d3005b85d
@ -10880,7 +10880,8 @@ namespace ts {
|
||||
return booleanType;
|
||||
case SyntaxKind.PlusPlusToken:
|
||||
case SyntaxKind.MinusMinusToken:
|
||||
const ok = checkArithmeticOperandType(node.operand, operandType, Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type);
|
||||
const ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType),
|
||||
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type);
|
||||
if (ok) {
|
||||
// run check only if former checks succeeded to avoid reporting cascading errors
|
||||
checkReferenceExpression(node.operand,
|
||||
@ -10894,7 +10895,8 @@ namespace ts {
|
||||
|
||||
function checkPostfixUnaryExpression(node: PostfixUnaryExpression): Type {
|
||||
const operandType = checkExpression(node.operand);
|
||||
const ok = checkArithmeticOperandType(node.operand, operandType, Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type);
|
||||
const ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType),
|
||||
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type);
|
||||
if (ok) {
|
||||
// run check only if former checks succeeded to avoid reporting cascading errors
|
||||
checkReferenceExpression(node.operand,
|
||||
@ -11148,6 +11150,9 @@ namespace ts {
|
||||
if (leftType.flags & TypeFlags.Undefined) leftType = rightType;
|
||||
if (rightType.flags & TypeFlags.Undefined) rightType = leftType;
|
||||
|
||||
leftType = getNonNullableType(leftType);
|
||||
rightType = getNonNullableType(rightType);
|
||||
|
||||
let suggestedOperator: SyntaxKind;
|
||||
// if a user tries to apply a bitwise operator to 2 boolean operands
|
||||
// try and return them a helpful suggestion
|
||||
@ -11176,6 +11181,9 @@ namespace ts {
|
||||
if (leftType.flags & TypeFlags.Undefined) leftType = rightType;
|
||||
if (rightType.flags & TypeFlags.Undefined) rightType = leftType;
|
||||
|
||||
leftType = getNonNullableType(leftType);
|
||||
rightType = getNonNullableType(rightType);
|
||||
|
||||
let resultType: Type;
|
||||
if (isTypeOfKind(leftType, TypeFlags.NumberLike) && isTypeOfKind(rightType, TypeFlags.NumberLike)) {
|
||||
// Operands of an enum type are treated as having the primitive type Number.
|
||||
@ -11235,7 +11243,7 @@ namespace ts {
|
||||
case SyntaxKind.AmpersandAmpersandToken:
|
||||
return rightType;
|
||||
case SyntaxKind.BarBarToken:
|
||||
return getUnionType([leftType, rightType]);
|
||||
return getUnionType([getNonNullableType(leftType), rightType]);
|
||||
case SyntaxKind.EqualsToken:
|
||||
checkAssignmentOperator(rightType);
|
||||
return getRegularTypeOfObjectLiteral(rightType);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user