Skip to content

fix: preserve parentheses around {#each} TypeScript as assertions - #548

Closed
cpruijsen wants to merge 1 commit into
sveltejs:mainfrom
cpruijsen:fix/issue-486
Closed

cpruijsen wants to merge 1 commit into
sveltejs:mainfrom
cpruijsen:fix/issue-486

Conversation

@cpruijsen

Copy link
Copy Markdown

Summary

  • {#each (Object.entries(modes) as [Mode, string][]) as [id, name]} was printed as {#each Object.entries(modes) as [Mode, string][] as [id, name]}. The compiler drops grouping parentheses from the each-block expression AST, and Prettier then omits them as unnecessary in JS, so the TypeScript as is concatenated into the {#each} as context keyword.
  • Wrap {#each} expressions whose AST type is TSAsExpression so the assertion stays parenthesized.
  • Fixes Formatter removes parentheses from the each block types #486.

Decision

  • Chose: wrap when node.expression.type === 'TSAsExpression'.
  • Alternative: preserve every user-written grouping parenthesis around each-block expressions (or also wrap satisfies / angle-bracket assertions).
  • Why: same idea as {#each (a, b) as item}, only wrap when {#each}'s as would otherwise collide with TypeScript as. Sequence expressions already get parens from Prettier; TSAsExpression is the hole. Happy to widen the predicate if you want that.

The bind:value={((x as T).y as U).z} case from the issue comment is already handled by the existing bind: parentheses fix (#501 / #503); this PR does not change that path.

Test plan

  • test/printer/samples/each-block-type-assertion.html fails without the wrap and passes with it
  • Existing {#each} printer samples, typescript-template, and pnpm test still pass

The compiler drops grouping parens from each-block expressions, and
Prettier then omits them as unnecessary in JS, which collides with the
{#each} as context keyword.

Fixes sveltejs#486
@cpruijsen

Copy link
Copy Markdown
Author

Closing this. Not something I'm going to keep current.

@cpruijsen cpruijsen closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter removes parentheses from the each block types

1 participant