[TypeScript] Typing a variable holding a component as value
See original GitHub issueWhen 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:
import AnyComponent from "./AnyComponent"
const componentStore = writable<SvelteComponent>(null)
$componentStore = AnyComponent
The last line will give the following error:
Type ‘typeof <component>__SvelteComponent_’ is missing the following properties from type ‘SvelteComponentDev’: $set, $on, $destroy, $capture_state, and 2 more.
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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:17 (4 by maintainers)
Top Results From Across the Web
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >Your reference guide to using TypeScript in React
Here's a quick reference guide where you can learn about best practices and generic TypeScript types in React.
Read more >How to Use TypeScript with React Components
In this post, I'm going to discuss why and how to use TypeScript to type React components. You'll find how to annotate component...
Read more >ReactJS and Typescript : refers to a value, but is being used ...
When you create a class in TypeScript, the name of that class refers to both the instance type and the Javascript class value....
Read more >Using TypeScript with React, the Syntax - DEV Community
JavaScript is a dynamically typed language. Meaning that you can declare a variable of one type of value, say a string, then reassign...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi there 😄 One thing is that we are missing some properties/methods on our internal definition of our Svelte component class. The other thing is that the typing is wrong, it should be
writable<typeof SvelteComponent>
, else you are refering to the instance of that component, not the class type.I don’t know why it works with
type
and notinterface
, but instead oftypeof SvelteComponent
writenew (...args: any[]) => SvelteComponent