TypeScript prevents arithmetic operations on invalid types like strings, booleans, and Date objects. This error enforces type safety by requiring operands to be number, bigint, enum, or any types.
This error occurs when you attempt an arithmetic operation (addition, subtraction, multiplication, division, etc.) with operands that TypeScript doesn't consider valid for such operations. TypeScript's strict type checking ensures you only perform arithmetic on types that support mathematical operations. Invalid types include strings, booleans, objects, and Date objects, even though JavaScript allows implicit type coercion with some of these.
If you have string operands, explicitly convert them to numbers before performing arithmetic operations:
// Incorrect - causes TS2363 error
const result = 5 - '3';
// Correct - convert to number first
const result = 5 - Number('3');
const result2 = parseInt('10') + 5;
const result3 = parseFloat('3.14') * 2;Use Number() for general conversion, parseInt() for integers, or parseFloat() for decimal numbers.
When performing arithmetic with Date objects, convert them to numbers using getTime() which returns milliseconds since Unix epoch:
// Incorrect - causes TS2363 error
const diff = new Date() - new Date('2024-01-01');
// Correct - use getTime()
const diff = new Date().getTime() - new Date('2024-01-01').getTime();
// Or use valueOf()
const diff2 = new Date().valueOf() - new Date('2024-01-01').valueOf();When declaring variables used in arithmetic, explicitly type them as number or bigint:
// Incorrect - implicit string type
const value = '42';
const result = 10 + value; // TS2363 error
// Correct - explicitly type as number
const value: number = 42;
const result = 10 + value;
// Or convert at usage
const value = '42';
const result = 10 + Number(value);If a variable has a union type that includes non-numeric types, use type guards before arithmetic:
// Incorrect - 'value' could be string
const value: number | string = getValue();
const result = value + 5; // TS2363 error
// Correct - type guard or assertion
const value: number | string = getValue();
if (typeof value === 'number') {
const result = value + 5; // Safe now
}
// Or convert
const result = Number(value) + 5;If working with very large integers beyond JavaScript's safe integer limit, use bigint type:
// Regular number (safe up to 2^53 - 1)
const smallNum: number = 42;
const result1 = smallNum + 5;
// BigInt for larger numbers
const largeNum: bigint = 9007199254740991n;
const result2 = largeNum + 5n; // Must use bigint literals (n suffix)
// Can't mix number and bigint
const mixed = smallNum + largeNum; // TS2363 error - convert first
const correct = BigInt(smallNum) + largeNum;This is a deliberate type safety feature in TypeScript. Unlike JavaScript, which coerces types in arithmetic operations (e.g., '5' - 3 = 2), TypeScript requires explicit conversion to prevent subtle bugs. If you're migrating JavaScript to TypeScript, you may need to add type conversions throughout your codebase. Recent TypeScript versions (5.5+) improved handling of some edge cases, so consider upgrading if you encounter unexpected restrictions. For custom objects with numeric meaning, implement the valueOf() method, though TypeScript still won't allow direct arithmetic—you'll need to call valueOf() explicitly.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript