You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently values shared with getContext/setContext are losing their type information.
If have a simple proposal to share the types getween getContext and setContext via the used key.
So that the value returned by getContext for a certain key has the same type as the value provided with setContext for this key.
I am also willing to work on a pull request for this feature, if there is interest to have it in svelte.
Describe the proposed solution
Here is my proposed solution for the interface (similiar to vues provide/inject)
// svelte patchimport{getContextasorigGetContext,setContextasorigSetContext}from"svelte";classContextKey<T>{}functiongetContext<T>(key: ContextKey<T>): T{returnorigGetContext(key);}functionsetContext<T>(key: ContextKey<T>,value: T){origSetContext(key,value);}// define available context keys in own moduleconstkeyA=newContextKey<number>();// use setContext/getContext with typingsetContext(keyA,100);// oksetContext(keyA,"FOOBAR");// ts errorconstn: number=getContext(keyA);// okconsts: string=getContext(keyA);// ts error
Importance
would make my life easier
The text was updated successfully, but these errors were encountered:
Describe the problem
Currently values shared with
getContext/setContext
are losing their type information.If have a simple proposal to share the types getween
getContext
andsetContext
via the used key.So that the value returned by
getContext
for a certain key has the same type as the value provided withsetContext
for this key.I am also willing to work on a pull request for this feature, if there is interest to have it in svelte.
Describe the proposed solution
Here is my proposed solution for the interface (similiar to vues provide/inject)
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: