question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TypeScript] Typing a variable holding a component as value

See original GitHub issue

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:

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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
dummdidummcommented, Aug 26, 2020

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.

6reactions
dummdidummcommented, Jun 26, 2021

I don’t know why it works with type and not interface, but instead of typeof SvelteComponent write new (...args: any[]) => SvelteComponent

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found