How to type the context from the docs example
Using the example from here I'm trying to get it working in TS, but TS doesn't seem to understand
<CounterContext.Provider
and throws all kinds of errors.
Does anyone know how to make this work?
13 Replies
The TS errors, if that helps
Change the file extension from
ts
to tsx
:facepalm:
Hello, I hope you don't mind me piggy-backing on this discussion. I am trying to use Context in TypeScript but I can't seem to figure out how to keep the compiler happy.
Here is my attempt:
What am I doing wrong?
You didn't specify the type for the context on
createContext
.
You have to do this:
value
is erroring with:
Here's the playground: https://playground.solidjs.com/anonymous/8b3a8f18-f97d-444a-aa9b-4071dcbc3431
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
add
satisfies CounterContextProps
to the counter definitionSolid Playground
Quickly discover what the solid compiler will generate from your JSX template
While that takes care of the error in
value
, there's another error when I try to use the context. Please take a look at your playground.
I want to use Context in a typesafe way, but it feels like I am missing some knowledge.Oh sorry I missed that
It's an array but you're trying to destructure as an object
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Thank you, I am beginning to understand how the typing works.
I have a question: can I use SolidJS stores inside a context? I have tried but cannot seem to get past the TypeScript errors. Here is my attempt:
https://playground.solidjs.com/anonymous/0375fc53-4580-46fa-b409-546982454524
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template