Skip to content

TypeScript type assertions lose opening parenthesis in bind:value expressions, causing syntax errors #501

@beshanoe

Description

@beshanoe

TypeScript type assertions lose opening parenthesis in bind:value expressions, causing syntax errors

Bug Description

When using Prettier with prettier-plugin-svelte, TypeScript type assertions in bind:value expressions lose their opening parenthesis, resulting in invalid syntax that breaks parsing.

Example

Before formatting (correct syntax):

<script lang="ts">
  interface Config {
    value: string;
  }
  
  let config: Config | Record<string, never> = { value: 'test' };
</script>

<input bind:value={(config as Config).value} />

After prettier --write (broken syntax):

<script lang="ts">
  interface Config {
    value: string;
  }

  let config: Config | Record<string, never> = { value: 'test' };
</script>

<input bind:value={config as Config).value} />

Notice the missing opening parenthesis: (config as Config) becomes config as Config).

Impact

This creates a cascade of problems:

  1. Syntax Error: The code becomes unparseable due to mismatched parentheses
  2. ESLint Failure: eslint --fix breaks the file, making it impossible to lint on subsequent runs
  3. Build Failure: Svelte compiler cannot parse the invalid syntax

Environment

  • prettier: 3.6.2
  • prettier-plugin-svelte: 3.4.0 (latest)
  • svelte: 5.0.0
  • Node.js: 20.19.4
  • npm: 10.8.2

Reproduction

  1. Create a .svelte file with TypeScript type assertion in bind:value
  2. Run prettier --write file.svelte
  3. Observe the missing opening parenthesis

Minimal Reproduction Case

<script lang="ts">
  interface Config { value: string; }
  let config: Config | Record<string, never> = { value: 'test' };
</script>

<input bind:value={(config as Config).value} />

Run: prettier --write file.svelte

Result: bind:value={config as Config).value} (missing opening parenthesis)

Related Issues

This appears related to:

Expected Behavior

Prettier should preserve the parentheses around TypeScript type assertions in bind:value expressions to maintain valid syntax.

Workaround

Use <!-- prettier-ignore --> comments before affected lines:

<!-- prettier-ignore -->
<input bind:value={(config as Config).value} />

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions