diff --git a/.changeset/dull-laws-end.md b/.changeset/dull-laws-end.md new file mode 100644 index 0000000000..595424826e --- /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 5febef420d..36a25f5124 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, diff --git a/packages/svelte/tests/runtime-runes/samples/typescript/_config.js b/packages/svelte/tests/runtime-runes/samples/typescript/_config.js index 7c9552b7b2..c15cd18225 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: '', diff --git a/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte b/packages/svelte/tests/runtime-runes/samples/typescript/main.svelte index 6c9cacdeff..71638688f2 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; }