Skip to content

fix: parenthesize TypeScript casts used as destructuring default targets - #256

Open
MFA-G wants to merge 1 commit into
sveltejs:mainfrom
MFA-G:fix/cast-target-in-destructuring-default
Open

MFA-G wants to merge 1 commit into
sveltejs:mainfrom
MFA-G:fix/cast-target-in-destructuring-default

Conversation

@MFA-G

@MFA-G MFA-G commented Oct 21, 2026

Copy link
Copy Markdown

Closes #253

AssignmentPattern printed its left as-is, so a TypeScript cast used as the target of a destructuring default lost the parentheses it needs:

({ a: (value as any) = 1 } = {});
// printed as
({ a: value as any = 1 } = {}); // TS: ',' expected.

#218 already wraps casts on the left of an AssignmentExpression. This moves that check into a small is_cast helper and uses it in AssignmentPattern too, so as, satisfies and <T>x targets keep their parentheses in object and array patterns. Defaults without a cast ({ a = 1 }, [b = 2], c = 3) and casts on the right-hand side ({ a = 1 as any }) print as before.

Tests

  • Added cases to test/samples/ts-as-precedence covering an object pattern, an array pattern, a member target, satisfies and an angle-bracket assertion, and updated the snapshot and map.
  • Also checked the printed code with ts.createSourceFile and oxc-parser: without the change, the five cast cases from the issue fail to parse in TypeScript; with it they all parse.
  • pnpm test (516 passed), pnpm check and pnpm lint pass.
  • Changeset included (patch).

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of esrap from 19c7929:

pnpm add https://pkg.svelte.dev/esrap/c/19c7929db3eaf75bd18ccc83f39538adc360cc7d

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/esrap/pr/256

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Oct 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 19c7929

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
esrap Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

TypeScript cast targets lose required parentheses in destructuring defaults

1 participant