Repository: sveltejs/svelte (JavaScript)

Issue:
`as const` assertion fails inside class properties in module scope
### Describe the bug

I don't know if i am in the correct repo or if [esrap](https://github.com/Rich-Harris/esrap) directly is better. However, since it concerns svelte and the stacktrace throws vite-plugin-svelte as the cause, I'll open an issue here. Otherwise i can simply move it.

Using `as const` type assertion in a class inside a`<script module lang="ts">` produces `Internal server error: Not implemented TSAsExpression`. It is possible in a `.svelte.ts`file tho.

### Reproduction URL

https://stackblitz.com/edit/stackblitz-starters-bsfhwa?file=src%2Froutes%2F%2Bpage.svelte

### Reproduction

1. Use a `<script module lang="ts">` in a `.svelte`Component
2. Create a class in the module
3. assert `as const` to a property
4. Error: `Not implemented TSAsExpression`

```ts
//comp.svelte
<script module lang="ts">
    const foo = "bar" as const //works
    class Test {
        foo = "bar" as const // breaks : Not implemented TSAsExpression
    }
</script>
```
```ts
//script.svelte.ts
    const foo = "bar" as const // works
    class Test {
        foo = "bar" as const // works
    }
```

### Logs

```shell
Internal server error: Not implemented TSAsExpression
  Plugin: vite-plugin-svelte
  File: /home/projects/stackblitz-starters-bsfhwa/src/routes/+page.svelte
      at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:62:9)
      at PropertyDefinition (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:1084:4)
      at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:70:2)
      at handle_body (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:268:3)
      at BlockStatement|ClassBody (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:463:3)
      at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:70:2)
      at ClassDeclaration|ClassExpression (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:567:3)
      at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:70:2)
      at handle_body (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:268:3)
      at Program (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:1044:3) (x2)
{
  name: 'Error',
  id: '/home/projects/stackblitz-starters-bsfhwa/src/routes/+page.svelte',
  message: 'Not implemented TSAsExpression',
  frame: '',
  code: undefined,
  stack: 'Error: Not implemented TSAsExpression\n' +
    '    at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:62:9)\n' +
    '    at PropertyDefinition (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:1084:4)\n' +
    '    at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:70:2)\n' +
    '    at handle_body (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:268:3)\n' +
    '    at BlockStatement|ClassBody (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:463:3)\n' +
    '    at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:70:2)\n' +
    '    at ClassDeclaration|ClassExpression (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:567:3)\n' +
    '    at handle (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:70:2)\n' +
    '    at handle_body (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:268:3)\n' +
    '    at Program (file:///home/projects/stackblitz-starters-bsfhwa/node_modules/.pnpm/esrap@1.2.2/node_modules/esrap/src/handlers.js:1044:3)',
  plugin: 'vite-plugin-svelte',
  pluginCode: '<script module lang="ts">\n' +
    '  const foo = "bar" as const //works\n' +
    '\tclass Test {\n' +
    '\t\tfoo = "bar" as const // breaks\n' +
    '\t}\n' +
    '</script>\n'
}
```


### System Info

```shell
System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.3.1 
    @sveltejs/kit: ^2.0.0 => 2.7.7 
    @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.0 
    svelte: ^5.0.0 => 5.1.10 
    vite: ^5.0.3 => 5.4.10
```



Discussion:
you have to enable typescript script preprocessor for this i think.

https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md#enable-script-preprocessing-for-advanced-typescript-syntax-use

You're totally right, that indeed works, i didn't know that-  but it's pretty odd that `as const` works in the module scope and in .svelte.ts files, but breaks inside classes without the TypeScript preprocessor enabled. While enabling it fixes the issue, this inconsistent behavior is quite confusing. What do you think?

Furthermore: it's just a type assertion and doesn't emit any runtime code



Patch:
diff --git a/.changeset/dull-laws-end.md b/.changeset/dull-laws-end.md
new file mode 100644
index 000000000000..595424826e7c
--- /dev/null
+++ b/.changeset/dull-laws-end.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: ignore `as` type expressions on property definitions
diff --git a/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js b/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js
index 5febef420d97..36a25f512445 100644
--- a/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js
+++ b/packages/svelte/src/compiler/phases/1-parse/remove_typescript_nodes.js
@@ -56,13 +56,14 @@ const visitors = {
 		if (node.exportKind === 'type') return b.empty;
 		return node;
 	},
-	PropertyDefinition(node) {
+	PropertyDefinition(node, { next }) {
 		if (node.accessor) {
 			e.typescript_invalid_feature(
 				node,
 				'accessor fields (related TSC proposal is not stage 4 yet)'
 			);
 		}
+		return next();
 	},
 	TSAsExpression(node, context) {
 		return context.visit(node.expression);
@@ -97,7 +98,7 @@ const visitors = {
 		if ((node.readonly || node.accessibility) && context.path.at(-2)?.kind === 'constructor') {
 			e.typescript_invalid_feature(node, 'accessibility modifiers on constructor parameters');
 		}
-		return node.parameter;
+		return context.visit(node.parameter);
 	},
 	FunctionExpression: remove_this_param,
 	FunctionDeclaration: remove_this_param,


Test patch:
diff --git a/packages/svelte/tests/runtime-runes/samples/typescript/_config.js b/packages/svelte/tests/runtime-runes/samples/typescript/_config.js
index 7c9552b7b21f..c15cd1822553 100644
--- a/packages/svelte/tests/runtime-runes/samples/typescript/_config.js
+++ b/packages/svelte/tests/runtime-runes/samples/typescript/_config.js
@@ -1,6 +1,7 @@
 import { flushSync } from 'svelte';
 import { test } from '../../test';
 
+// This test mainly checks that all types are properly ignored by the compiler
 export default test({
 	html: '<button>clicks: 0</button>',
 
diff --git a/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte b/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte
index 6c9cacdeff27..71638688f2d8 100644
--- a/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte
+++ b/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte
@@ -10,6 +10,7 @@
 
 	class Foo {
 		public name: string;
+		x = 'x' as const;
 		constructor(name: string) {
 			this.name = name;
 		}