Skip to content

Remove "Did you forget to use await" for unary arithmetic expressions #32334

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
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
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23713,9 +23713,9 @@ namespace ts {
}
}

function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage): boolean {
function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage, isAwaitValid = false): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sold on the parameter name. Seems more like 'suggestAwait' or something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether await is actually suggested is left up to some additional checks performed in the function; it could be trySuggestAwait or maybeSuggestAwait or isAwaitValid. I think the last sounds best. Thanks 👍

if (!isTypeAssignableTo(type, numberOrBigIntType)) {
const awaitedType = getAwaitedType(type);
const awaitedType = isAwaitValid && getAwaitedTypeOfPromise(type);
errorAndMaybeSuggestAwait(
operand,
!!awaitedType && isTypeAssignableTo(awaitedType, numberOrBigIntType),
Expand Down Expand Up @@ -24327,8 +24327,8 @@ namespace ts {
}
else {
// otherwise just check each operand separately and report errors as normal
const leftOk = checkArithmeticOperandType(left, leftType, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type);
const rightOk = checkArithmeticOperandType(right, rightType, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type);
const leftOk = checkArithmeticOperandType(left, leftType, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, /*isAwaitValid*/ true);
const rightOk = checkArithmeticOperandType(right, rightType, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type, /*isAwaitValid*/ true);
let resultType: Type;
// If both are any or unknown, allow operation; assume it will resolve to number
if ((isTypeAssignableToKind(leftType, TypeFlags.AnyOrUnknown) && isTypeAssignableToKind(rightType, TypeFlags.AnyOrUnknown)) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ tests/cases/compiler/operationsAvailableOnPromisedType.ts(27,5): error TS2349: T
b++;
~
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:15:5: Did you forget to use 'await'?
--b;
~
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:16:7: Did you forget to use 'await'?
a === b;
~~~~~~~
!!! error TS2367: This condition will always return 'false' since the types 'number' and 'Promise<number>' have no overlap.
Expand Down