@@ -12332,16 +12332,18 @@ namespace ts {
12332
12332
}
12333
12333
12334
12334
function checkNonNullExpression(node: Expression | QualifiedName) {
12335
- const type = checkExpression(node);
12336
- if (strictNullChecks) {
12337
- const kind = getFalsyFlags(type) & TypeFlags.Nullable;
12338
- if (kind) {
12339
- error(node, kind & TypeFlags.Undefined ? kind & TypeFlags.Null ?
12340
- Diagnostics.Object_is_possibly_null_or_undefined :
12341
- Diagnostics.Object_is_possibly_undefined :
12342
- Diagnostics.Object_is_possibly_null);
12343
- }
12344
- return getNonNullableType(type);
12335
+ return checkNonNullType(checkExpression(node), node);
12336
+ }
12337
+
12338
+ function checkNonNullType(type: Type, errorNode: Node): Type {
12339
+ const kind = (strictNullChecks ? getFalsyFlags(type) : type.flags) & TypeFlags.Nullable;
12340
+ if (kind) {
12341
+ error(errorNode, kind & TypeFlags.Undefined ? kind & TypeFlags.Null ?
12342
+ Diagnostics.Object_is_possibly_null_or_undefined :
12343
+ Diagnostics.Object_is_possibly_undefined :
12344
+ Diagnostics.Object_is_possibly_null);
12345
+ const t = getNonNullableType(type);
12346
+ return t.flags & (TypeFlags.Nullable | TypeFlags.Never) ? unknownType : t;
12345
12347
}
12346
12348
return type;
12347
12349
}
@@ -14880,17 +14882,9 @@ namespace ts {
14880
14882
if (leftType === silentNeverType || rightType === silentNeverType) {
14881
14883
return silentNeverType;
14882
14884
}
14883
- // TypeScript 1.0 spec (April 2014): 4.19.1
14884
- // These operators require their operands to be of type Any, the Number primitive type,
14885
- // or an enum type. Operands of an enum type are treated
14886
- // as having the primitive type Number. If one operand is the null or undefined value,
14887
- // it is treated as having the type of the other operand.
14888
- // The result is always of the Number primitive type.
14889
- if (leftType.flags & TypeFlags.Nullable) leftType = rightType;
14890
- if (rightType.flags & TypeFlags.Nullable) rightType = leftType;
14891
14885
14892
- leftType = getNonNullableType (leftType);
14893
- rightType = getNonNullableType (rightType);
14886
+ leftType = checkNonNullType (leftType, left );
14887
+ rightType = checkNonNullType (rightType, right );
14894
14888
14895
14889
let suggestedOperator: SyntaxKind;
14896
14890
// if a user tries to apply a bitwise operator to 2 boolean operands
@@ -14915,16 +14909,11 @@ namespace ts {
14915
14909
if (leftType === silentNeverType || rightType === silentNeverType) {
14916
14910
return silentNeverType;
14917
14911
}
14918
- // TypeScript 1.0 spec (April 2014): 4.19.2
14919
- // The binary + operator requires both operands to be of the Number primitive type or an enum type,
14920
- // or at least one of the operands to be of type Any or the String primitive type.
14921
-
14922
- // If one operand is the null or undefined value, it is treated as having the type of the other operand.
14923
- if (leftType.flags & TypeFlags.Nullable) leftType = rightType;
14924
- if (rightType.flags & TypeFlags.Nullable) rightType = leftType;
14925
14912
14926
- leftType = getNonNullableType(leftType);
14927
- rightType = getNonNullableType(rightType);
14913
+ if (!isTypeOfKind(leftType, TypeFlags.Any | TypeFlags.StringLike) && !isTypeOfKind(rightType, TypeFlags.Any | TypeFlags.StringLike)) {
14914
+ leftType = checkNonNullType(leftType, left);
14915
+ rightType = checkNonNullType(rightType, right);
14916
+ }
14928
14917
14929
14918
let resultType: Type;
14930
14919
if (isTypeOfKind(leftType, TypeFlags.NumberLike) && isTypeOfKind(rightType, TypeFlags.NumberLike)) {
0 commit comments