-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow the use of TypeScript mixins with <svelte:options extend /> #9032
Comments
It would seem that you can still use mixins if the mixins themselves are written in a plain JavaScript file rather than in a TypeScript file. So this is actually just a bug specific to TypeScript. That downgrades the "Importance" here from "would make my life easier" to a "nice to have". |
@willnationsdev I'd like to explain a bit about Svelte's architecture so that you can get the big picture about what works and what not. Svelte only understands JS, it supports other languages (TS included) through preprocessor that compiles into JS. Internally it uses acorn to parse expressions between curly braces, and since acorn is a JS parser, those epxressions must be plain JS. To each component file there're 3 parts that're pre-processable: markup, script, and style. Although you can write TS between This architecture is highly unlikely to change in any foreseeable future, and out of the scope of Svelte core compiler. If it's important to you, you might want to ask for a markup preprocessor that handles TS between curly braces in the svelte-preprocess repo instead. Meanwhile, a easy workaround is to write your extend function inside script tag and reference it in markup. <svelte:options
customElement={{
tag: "my-input",
extend: extendElement,
}}
/>
<script lang="ts">
import { FormItem } from "./mixins";
function extendElement(ctor: new () => HTMLElement) {
return class extends FormItem(ctor) {
constructor(...args: any[]) {
super(...args);
}
}
}
</script> |
@hackape Thanks. After I made the comment, I forked Svelte and investigated it myself and deduced much of what you said above. The thing is, I coulda sworn I encountered this problem even after I moved the logic to a dedicated .ts file. I'll try again tomorrow and see if I can reproduce the issue or if it was just my bad memory. Either way, if the Svelte options parser is a JS parser, then there is indeed no reason to even consider this a bug, provided I can verify the linked mixin works as expected. |
This is essentially a duplicate of #4701 - closing in favor of that. Right now you can't use TS inside the template, and this is counted as part of the template technically speaking. |
Describe the problem
Related to #4168.
I thought I could work around the lack of base type overrides by at least using mixins to automatically establish some properties on components from a single location using a mixin, like so:
Then, import and use those mixins in the
extend
function, like so:Describe the proposed solution
I'd like to be able to adjust the
extend
lambda so that it looks like this:Alternatives considered
As far as I know (based on the GitHub PR), this is a requirement of the language, so I don't think there are any alternatives available.
As it stands, I have to copy/paste the same base class logic in every single custom element I make with Svelte components that happen to share functionality (not ideal, obviously).
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: