Skip to content

as/satisfies between exponentiation operators cannot be safely erased #63661

Description

@magic-akari

🔎 Search Terms

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?erasableSyntaxOnly=true#code/PTAEAEFMCcEMGdYCMA2kDKBPAdgF1gB4Dy2KmAXKLtAK6QBQ9AxgPbby6gIBqsKdoALygATKABU40AGYu8UNhoBbJDAlSRAbmZsOoRLgCW8AGaHI8Xv0hDR6mfthHT5+YpVrJozUA

💻 Code

const asValue = 2 ** 3 as number ** 2;
const satisfiesValue = 2 ** 3 satisfies number ** 2;

🙁 Actual behavior

Both expressions are accepted without a diagnostic.

TypeScript emits each expression with the left-hand exponentiation parenthesized:

const asValue = (2 ** 3) ** 2;
const satisfiesValue = (2 ** 3) ** 2;

Replacing only the type syntax with spaces instead produces the equivalent of:

const asValue = 2 ** (3 ** 2);
const satisfiesValue = 2 ** (3 ** 2);

The emitted expression evaluates to 64, while the blanked expression evaluates to 512.

🙂 Expected behavior

TypeScript should report an error for these expressions because erasing the type syntax without moving source positions changes the exponentiation grouping. This should apply to both as and satisfies, consistent with the conclusion of #63527.

Additional information about the issue

Exponentiation is right-associative. The two ** operators have equal precedence, but removing the intervening type syntax changes the expression from left-grouped to right-grouped.

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: ParserThe parser didn't handle the syntax correctly

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions