Skip to content

check base constraint when checking operand of plus #49918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -33430,7 +33430,7 @@ namespace ts {
error(node.operand, Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, tokenToString(node.operator));
}
if (node.operator === SyntaxKind.PlusToken) {
if (maybeTypeOfKind(operandType, TypeFlags.BigIntLike)) {
if (maybeTypeOfKindConsideringBaseConstraint(operandType, TypeFlags.BigIntLike)) {
error(node.operand, Diagnostics.Operator_0_cannot_be_applied_to_type_1, tokenToString(node.operator), typeToString(getBaseTypeOfLiteralType(operandType)));
}
return numberType;
17 changes: 15 additions & 2 deletions tests/baselines/reference/numberVsBigIntOperations.errors.txt
Original file line number Diff line number Diff line change
@@ -62,9 +62,11 @@ tests/cases/compiler/numberVsBigIntOperations.ts(61,1): error TS2365: Operator '
tests/cases/compiler/numberVsBigIntOperations.ts(70,2): error TS2736: Operator '+' cannot be applied to type 'number | bigint'.
tests/cases/compiler/numberVsBigIntOperations.ts(86,7): error TS1155: 'const' declarations must be initialized.
tests/cases/compiler/numberVsBigIntOperations.ts(93,7): error TS1155: 'const' declarations must be initialized.
tests/cases/compiler/numberVsBigIntOperations.ts(98,6): error TS2736: Operator '+' cannot be applied to type 'S'.
tests/cases/compiler/numberVsBigIntOperations.ts(99,5): error TS2365: Operator '+' cannot be applied to types 'number' and 'S'.


==== tests/cases/compiler/numberVsBigIntOperations.ts (64 errors) ====
==== tests/cases/compiler/numberVsBigIntOperations.ts (66 errors) ====
// Cannot mix bigints and numbers
let bigInt = 1n, num = 2;
bigInt = 1n; bigInt = 2; num = 1n; num = 2;
@@ -286,4 +288,15 @@ tests/cases/compiler/numberVsBigIntOperations.ts(93,7): error TS1155: 'const' de
const bigZeroOrOne: 0n | 1;
~~~~~~~~~~~~
!!! error TS1155: 'const' declarations must be initialized.
if (bigZeroOrOne) isOne(bigZeroOrOne);
if (bigZeroOrOne) isOne(bigZeroOrOne);

type NumberOrBigint = number | bigint;
function getKey<S extends NumberOrBigint>(key: S) {
+key; // should error
~~~
!!! error TS2736: Operator '+' cannot be applied to type 'S'.
0 + key; // should error
~~~~~~~
!!! error TS2365: Operator '+' cannot be applied to types 'number' and 'S'.
}

13 changes: 12 additions & 1 deletion tests/baselines/reference/numberVsBigIntOperations.js
Original file line number Diff line number Diff line change
@@ -92,7 +92,14 @@ else isNumber(zeroOrBigOne);
const isOne = (x: 1 | 1n) => x;
if (zeroOrBigOne) isOne(zeroOrBigOne);
const bigZeroOrOne: 0n | 1;
if (bigZeroOrOne) isOne(bigZeroOrOne);
if (bigZeroOrOne) isOne(bigZeroOrOne);

type NumberOrBigint = number | bigint;
function getKey<S extends NumberOrBigint>(key: S) {
+key; // should error
0 + key; // should error
}


//// [numberVsBigIntOperations.js]
// Cannot mix bigints and numbers
@@ -272,3 +279,7 @@ if (zeroOrBigOne)
const bigZeroOrOne;
if (bigZeroOrOne)
isOne(bigZeroOrOne);
function getKey(key) {
+key; // should error
0 + key; // should error
}
17 changes: 17 additions & 0 deletions tests/baselines/reference/numberVsBigIntOperations.symbols
Original file line number Diff line number Diff line change
@@ -348,3 +348,20 @@ if (bigZeroOrOne) isOne(bigZeroOrOne);
>isOne : Symbol(isOne, Decl(numberVsBigIntOperations.ts, 90, 5))
>bigZeroOrOne : Symbol(bigZeroOrOne, Decl(numberVsBigIntOperations.ts, 92, 5))

type NumberOrBigint = number | bigint;
>NumberOrBigint : Symbol(NumberOrBigint, Decl(numberVsBigIntOperations.ts, 93, 38))

function getKey<S extends NumberOrBigint>(key: S) {
>getKey : Symbol(getKey, Decl(numberVsBigIntOperations.ts, 95, 38))
>S : Symbol(S, Decl(numberVsBigIntOperations.ts, 96, 16))
>NumberOrBigint : Symbol(NumberOrBigint, Decl(numberVsBigIntOperations.ts, 93, 38))
>key : Symbol(key, Decl(numberVsBigIntOperations.ts, 96, 42))
>S : Symbol(S, Decl(numberVsBigIntOperations.ts, 96, 16))

+key; // should error
>key : Symbol(key, Decl(numberVsBigIntOperations.ts, 96, 42))

0 + key; // should error
>key : Symbol(key, Decl(numberVsBigIntOperations.ts, 96, 42))
}

17 changes: 17 additions & 0 deletions tests/baselines/reference/numberVsBigIntOperations.types
Original file line number Diff line number Diff line change
@@ -727,3 +727,20 @@ if (bigZeroOrOne) isOne(bigZeroOrOne);
>isOne : (x: 1n | 1) => 1n | 1
>bigZeroOrOne : 1

type NumberOrBigint = number | bigint;
>NumberOrBigint : number | bigint

function getKey<S extends NumberOrBigint>(key: S) {
>getKey : <S extends NumberOrBigint>(key: S) => void
>key : S

+key; // should error
>+key : number
>key : S

0 + key; // should error
>0 + key : any
>0 : 0
>key : S
}

8 changes: 7 additions & 1 deletion tests/cases/compiler/numberVsBigIntOperations.ts
Original file line number Diff line number Diff line change
@@ -93,4 +93,10 @@ else isNumber(zeroOrBigOne);
const isOne = (x: 1 | 1n) => x;
if (zeroOrBigOne) isOne(zeroOrBigOne);
const bigZeroOrOne: 0n | 1;
if (bigZeroOrOne) isOne(bigZeroOrOne);
if (bigZeroOrOne) isOne(bigZeroOrOne);

type NumberOrBigint = number | bigint;
function getKey<S extends NumberOrBigint>(key: S) {
+key; // should error
0 + key; // should error
}