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
[TypeScript] Typing a variable holding a component as value #486
Comments
Hi there 😄 |
with that of SvelteComponentDef #486
I have encountered a similar issue.
v 3.35.0 The question is, why an imported component has a type "typeof SvelteComponentDev" rather than SvelteComponentDev |
Because class Foo {
bar: string;
}
class Comp extends Foo {}
const comp: Foo = new Comp(); // OK
const compClass1: Foo = Comp; // WRONG
const compClass2: typeof Foo = Comp; // OK |
Ahh. Makes sense. Thank you. |
Fighting with this for 2 days straight now. I just don't get it: <script lang="ts">
import MyComponent from "./MyComponent.svelte";
let myComponent: typeof MyComponent;
</script>
<MyComponent bind:this={myComponent}/> //Error here Argument of type 'typeof MyComponent__SvelteComponent_' is not assignable to parameter of type 'MyComponent__SvelteComponent_'.
Type 'typeof MyComponent__SvelteComponent_' is missing the following properties from type 'MyComponent__SvelteComponent_': $$prop_def, $$events_def, $$slot_def, $on, and 5 more.ts(2345) What am i doing wrong here? I just want to get that Instance into a variable. 😭 |
@Alfagun74 the issue you are facing is not the same as others in this thread. What you want is an instance of the class. So you just need: <script lang="ts">
import MyComponent from "./MyComponent.svelte";
let myComponent: MyComponent;
</script>
<MyComponent bind:this={myComponent}/> Other people want a dynamic component so they need the class itself, a special function. |
I am a little confused about this situation: Let's say I want to have a property that can be any component, to be used in an array.
[...]
export declare class CheckCircle extends SvelteComponent<IconProps> {}
export declare class CheckSquare extends SvelteComponent<IconProps> {}
[...]
<script lang="ts">
import { CheckCircle, CheckSquare } from 'phosphor-svelte/lib';
interface ItemPanel {
icon: any;
text: string;
}
const items: ItemPanel[] = [
{
icon: CheckCircle,
text: 'Lorem Ipsum'
},
{
icon: CheckSquare,
text: 'Foo Bar'
}
];
</script>
{#each items as item}
<svelte:component this={item.icon} />
<h2>{item.text}</h2>
{/each} What type would you give to interface ItemPanel {
icon: typeof SvelteComponent;
text: string;
} but
I was wondering why it was wrong, and while messing around I tried to replace the interface with a type alias: type ItemPanel = {
icon: typeof SvelteComponent;
text: string;
}; ...and this one just works. I am relatively new to TS so maybe it's better to use types rather than interfaces for this kind of task, but I still don't understand why it gives me an error in an interface. I think I am going a bit out of scope here, but hopefully this can help anyone who has the same problem in the future. EDIT: I figured it out, and of course it's my bad... Whoops. import type { SvelteComponent } from 'phosphor-svelte/lib/shared'; instead of import type { SvelteComponent } from 'svelte'; That's what actually fixed it. |
I don't know why it works with |
Thanks, but seems that with the right import |
Hi I am having a similar issue |
@fourglobe302500 |
the file with the component is the components\SaveSelection.svelte |
The lib is the svelte dialogs |
Not sure about why this error is occurring in this particular context, I think you should create an issue in the svelte-dialogs repo. import type { SvelteComponentTyped } from 'svelte';
import type { Newable } from 'ts-essentials';
import SomeComp from './SomeComp.svelte';
type ComponentProps<T extends SvelteComponentTyped> =
T extends SvelteComponentTyped<infer R> ? R : unknown;
function openModal(open<Comp extends SvelteComponentTyped>(component: Newable<Comp>, props?: ComponentProps<Comp>) {
// ...
}
openModal(SomeComp, {
// The props are detected and you get code-completion, etc.
}); |
Thanks @aradalvand I will try it later and open the issue. |
@aradalvand found the problem though is a sneaky one Before my components was
But I was missing the slot parameter on the component
I only found it because I reread the documentation but this time on github that has the dark theme, npm deosn't have it |
I couldn't find a way to type an async function that returns a Svelte component. I tried with this: <script lang="ts">
import type { SvelteComponent } from 'svelte';
export let icon: () => Promise<typeof SvelteComponent>;
</script> But when I call it like this: <TextIcon icon={() => import('$lib/icons/Email.svelte')}>text</TextIcon> I get
Any idea? Here's a question at SO with the same issue Edit: someone answered at SO, I just had to type it like this <script lang="ts">
import type { ComponentType } from 'svelte';
export let icon: () => Promise<{ default: ComponentType }>;
</script> |
This is still randomly happening to me. I just created a new project using Vite and Svelte <!-- App.svelte -->
<script>
import Example from "./lib/Example.svelte";
import Smile from "./lib/Smile.svelte";
</script>
<Example icon={Smile} /> <!-- Example.svelte -->
<script lang="ts">
import { SvelteComponent } from "svelte";
export let icon: typeof SvelteComponent;
</script>
<svelte:component this={icon} /> <!-- Smile.svelte -->
😊 And I'm still getting this error:
If I change
|
For anyone struggling to type bindings of component instances: For some strange reason, Svelte Components we create ourselves is missing the I got the type to work with the Typescript
|
No. This is not correct. |
Ok, I understand now, thanks |
This code work for me |
Commenting this for visibility: TypeScript wasn't happy with Using |
I use this type to get type info of component methods for example (as an extension to the previous answer): import Cmp from './cmp.svelte'
let cmp: InstanceType<ComponentType<Cmp>> |
it solves my problem. thanks a lot. |
Thanks, |
When using TypeScript, it seems impossible to use a component as a type.
For instance, it is not possible to type a store to hold a component instance like so:
The last line will give the following error:
Maybe it is me that's missed something about how components are typed, but I would like to be able to use that kind of pattern.
Here is a StackOverflow post I opened before writing this issue, where @dummdidumm instructed me to open this issue. I hope that's ok.
The text was updated successfully, but these errors were encountered: