Skip to content

Erasing as or satisfies can expose an invalid left operand of **Β #65170

Closed

Description

@magic-akari

πŸ”Ž Search Terms

erasableSyntaxOnly, as, satisfies, exponentiation, unary expression, await

πŸ•— Version & Regression Information

Reproduced with TypeScript 6.0.3 and 7.0.2. The Playground uses 6.0.3 to demonstrate the missing diagnostic.

⏯ Playground Link

TypeScript Playground

πŸ’» Code

// @erasableSyntaxOnly: true
export function negate(x: number) {
    return -x as number ** 2;
}

export async function awaited(x: Promise<number>) {
    return await x as number ** 2;
}

πŸ™ Actual behavior

Both functions compile without diagnostics, including with --erasableSyntaxOnly. The native builds emit (-x) ** 2 and (await x) ** 2. Replacing the type syntax with spaces leaves -x ** 2 and await x ** 2, both of which Node.js rejects with a SyntaxError.

Replacing as number with satisfies number or using chained assertions reproduces the issue. Erasure also produces invalid JavaScript when it exposes an unparenthesized +, ~, !, typeof, void, or delete expression to the left of **.

πŸ™‚ Expected behavior

The parser already rejects related cases where erasing as or satisfies changes the grouping of a binary expression (#63527 and #63661). Reporting a parser diagnostic here would be consistent with those fixes. In these examples, erasure leaves an invalid left operand for **.

Even if these forms remain accepted by default, a diagnostic under --erasableSyntaxOnly would still catch the unsafe erasure.

Explicitly parenthesized forms such as (-x) as number ** 2 and (await x) as number ** 2 should still compile.

Additional information about the issue

Activity

  1. magic-akari commented on Nov 4, 2026

    @magic-akari
    ContributorAuthor

    What is the intended direction for fixing this? Should these expressions be rejected only with erasableSyntaxOnly, or should they be rejected in all configurations?

  2. RyanCavanaugh commented on Nov 4, 2026

    @RyanCavanaugh
    Member

    The best precedent here is #63533, where the treatment of 1 + 2 as any / 3 was discussed. So I think this is an error regardless of ESO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions