Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const stringOrNumberType = getUnionType([stringType, numberType]);
const stringNumberSymbolType = getUnionType([stringType, numberType, esSymbolType]);
const keyofConstraintType = keyofStringsOnly ? stringType : stringNumberSymbolType;
const numberOrBigIntType = getUnionType([numberType, bigintType]);
const templateConstraintType = getUnionType([stringType, numberType, booleanType, bigintType, nullType, undefinedType]) as UnionType;
const numericStringType = getTemplateLiteralType(["", ""], [numberType]); // The `${number}` type

Expand Down Expand Up @@ -2044,13 +2043,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let globalReadonlyArrayType: GenericType;
let globalStringType: ObjectType;
let globalNumberType: ObjectType;
let globalNumericValueOfType: ObjectType;
let globalBooleanType: ObjectType;
let globalRegExpType: ObjectType;
let globalThisType: GenericType;
let anyArrayType: Type;
let autoArrayType: Type;
let anyReadonlyArrayType: Type;
let deferredGlobalNonNullableTypeAlias: Symbol;
let numberOrBigIntType: Type;

// The library files are only loaded when the feature is used.
// This allows users to just specify library files they want to used through --lib
Expand Down Expand Up @@ -36342,8 +36343,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
const leftAssignableToNumber = isTypeAssignableTo(left, numberOrBigIntType);
const rightAssignableToNumber = isTypeAssignableTo(right, numberOrBigIntType);
return leftAssignableToNumber && rightAssignableToNumber ||
!leftAssignableToNumber && !rightAssignableToNumber && areTypesComparable(left, right);
if (leftAssignableToNumber && rightAssignableToNumber) return true;
const leftAssignableToString = isTypeAssignableTo(left, stringType);
const rightAssignableToString = isTypeAssignableTo(right, stringType);
return leftAssignableToString && rightAssignableToString;
});
}
return booleanType;
Expand Down Expand Up @@ -46215,6 +46218,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
globalNumberType = getGlobalType("Number" as __String, /*arity*/ 0, /*reportErrors*/ true);
globalBooleanType = getGlobalType("Boolean" as __String, /*arity*/ 0, /*reportErrors*/ true);
globalRegExpType = getGlobalType("RegExp" as __String, /*arity*/ 0, /*reportErrors*/ true);
globalNumericValueOfType = getGlobalType("NumericValueOf" as __String, /*arity*/ 0, /*reportErrors*/ false) || globalNumberType;
numberOrBigIntType = getUnionType([globalNumericValueOfType, bigintType]);
anyArrayType = createArrayType(anyType);

autoArrayType = createArrayType(autoType);
Expand Down
1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ export namespace Completion {
interfaceEntry("StringConstructor"),
varEntry("Boolean"),
interfaceEntry("BooleanConstructor"),
interfaceEntry("NumericValueOf"),
varEntry("Number"),
interfaceEntry("NumberConstructor"),
interfaceEntry("TemplateStringsArray"),
Expand Down
4 changes: 4 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ interface ObjectConstructor {
*/
declare var Object: ObjectConstructor;

interface NumericValueOf {
valueOf(): number;
}

/**
* Creates a new function.
*/
Expand Down
22 changes: 2 additions & 20 deletions tests/baselines/reference/arithmeticOnInvalidTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
tests/cases/compiler/arithmeticOnInvalidTypes.ts(3,9): error TS2365: Operator '+' cannot be applied to types 'Number' and 'Number'.
tests/cases/compiler/arithmeticOnInvalidTypes.ts(4,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/arithmeticOnInvalidTypes.ts(4,14): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/arithmeticOnInvalidTypes.ts(5,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/arithmeticOnInvalidTypes.ts(5,14): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/arithmeticOnInvalidTypes.ts(6,10): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/arithmeticOnInvalidTypes.ts(6,14): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.


==== tests/cases/compiler/arithmeticOnInvalidTypes.ts (7 errors) ====
==== tests/cases/compiler/arithmeticOnInvalidTypes.ts (1 errors) ====
var x: Number;
var y: Number;
var z = x + y;
~~~~~
!!! error TS2365: Operator '+' cannot be applied to types 'Number' and 'Number'.
var z2 = x - y;
~
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
var z3 = x * y;
~
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
var z4 = x / y;
~
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
var z4 = x / y;
Loading