Custom context in documents has a error in typescript
See original GitHub issueexport const DirectoryContext = React.createContext(null);
export const useDirectoryStore = createStoreHook(DirectoryContext);
export const useDirectoryDispatch = createDispatchHook(DirectoryContext);
export const useDirectorySelector = createSelectorHook(DirectoryContext);
createStoreHook(DirectoryContext)
has an error:
Argument of type 'Context<null>' is not assignable to parameter of type 'Context<ReactReduxContextValue<any, AnyAction>>'.
The types of 'Provider.propTypes' are incompatible between these types.
Type 'WeakValidationMap<ProviderProps<null>> | undefined' is not assignable to type 'WeakValidationMap<ProviderProps<ReactReduxContextValue<any, AnyAction>>> | undefined'.
Type 'WeakValidationMap<ProviderProps<null>>' is not assignable to type 'WeakValidationMap<ProviderProps<ReactReduxContextValue<any, AnyAction>>>'.
Types of property 'value' are incompatible.
Type 'Validator<null | undefined> | undefined' is not assignable to type 'Validator<ReactReduxContextValue<any, AnyAction>> | undefined'.
Type 'Validator<null | undefined>' is not assignable to type 'Validator<ReactReduxContextValue<any, AnyAction>>'.
Type 'null | undefined' is not assignable to type 'ReactReduxContextValue<any, AnyAction>'.
Type 'undefined' is not assignable to type 'ReactReduxContextValue<any, AnyAction>'.ts(2345)
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >How to use React Context with TypeScript - LogRocket Blog
Improve your development experience and learn how to use TypeScript with React Context in this post by building a to-do app from scratch....
Read more >JSDoc Reference - TypeScript: Documentation
JSDoc Reference. The list below outlines which constructs are currently supported when using JSDoc annotations to provide type information in JavaScript files.
Read more >create react app error compiling on windows where files have ...
It seems to work both ways though. This error suggests that the compiler is looking at a SearchContext.tsx file when it should be...
Read more >A Definitive Guide to Handling Errors in JavaScript - Kinsta
You might have forgotten to define a value for the variable before using it, or you might be trying to use an inaccessible...
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
@markerikson
I would suggest changing the docs then because this is the example context that’s used
So here we get into the issues of distinguishing what is legal runtime behavior, and what is legal compile-time behavior.
The initial value for a React context that you pass to
React.createContext()
will only be used at runtime if you try to consume the context without a corresponding<MyContext.Provider>
around it. In practical usage, this almost never happens. So, you can generally get away with definingcreateContext(null)
, because thatnull
will never be read from a context.However, TS doesn’t know that. TS expects that if you say a context value looks like
{a: number, b: string, c: SomeFancyCallbackType}
, that you will provide an object that looks like that at all times. That includes for the initial value.In plain JS code,
null
is an entirely acceptable value here. This also means that the current documentation is correct as-is, for plain JS usage, and that page does not try to discuss TS usage at all.It’s only TS that has to be satisfied. Therefore, the options are:
Last time I looked, our context looks roughly like
{store, subscription}
. I haven’t looked over at the TS typings in@types/react-redux
to see if they capture what we actually put into context.Therefore, my advice is to take option 2: just tell TS to shut up. Slap a
// @ts-ignore
or anas unknown as WhateverTheContextTypeIs
on it, and move on.