check base constraint when checking operand of plus (#49918)

This commit is contained in:
Zzzen
2022-07-18 09:42:50 -07:00
committed by GitHub
parent aa2b2352e1
commit efbe03a33c
6 changed files with 69 additions and 5 deletions
+1 -1
View File
@@ -33439,7 +33439,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;
@@ -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'.
}
@@ -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
}
@@ -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))
}
@@ -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
}
@@ -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
}