What is the base class for Svelte components?
See original GitHub issueI’m using Svelte with TypeScript, and I’ve got a function that should only accept a Svelte component as its argument. I want to be able to do something like this:
import Button from './Button.svelte';
myFunction(Button)
// or perhaps a generic version:
myFunction<Button>()
Needless to say, I want to enforce that the parameter must be a Svelte component, so that if anything else is passed, I get an error:
myFunction(123) // <= Should give an error
myFunction("Hello") // <= Should give an error
I initially intuitively thought it must be the SvelteComponent
class, but unfortunately, when I do the following:
import { SvelteComponent } from 'svelte';
import Button from './Button.svelte';
let foo: SvelteComponent = Button;
I get the following error:
Type 'typeof Button__SvelteComponent_' is missing the following properties from type 'SvelteComponentTyped<any, any, any>': $set, $on, $destroy, $$prop_def, and 5 more
SvelteComponentTyped
doesn’t seem to work either, same error:
import { SvelteComponentTyped } from 'svelte';
import Button from './Button.svelte';
let foo: SvelteComponentTyped = Button;
Imported from svelte/internal
, still doesn’t work; again, same error:
I’ve also created a simple GitHub repo that reproduces the error, in case you need it: https://github.com/AradAral/SvelteBaseClass
Is this intentional or a bug? If it’s intentional I think it can be misleading; and is there, therefore, no way to achieve what I just described in Svelte? Isn’t there any type one would use to enforce a Svelte component?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Turns out I was importing
SvelteComponent
fromsvelte/internal
when I was tryingtypeof SvelteComponent
. Thanks @dummdidumm, @PatrickG.For future reference, one is the type of an instance and the other is a function. The example only works because it is basically an empty object.