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.

Custom context in documents has a error in typescript

See original GitHub issue
export 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:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
trblackwcommented, May 29, 2020

@markerikson

I would expect that React.createContext(null) is indeed invalid

I would suggest changing the docs then because this is the example context that’s used

3reactions
markeriksoncommented, Jun 23, 2021

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 defining createContext(null), because that null 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:

  • Go through the entire exercise of defining an object that exactly matches the shape of what the context value is supposed to be
  • Tell TS to shut up and ignore what you’re doing here

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 an as unknown as WhateverTheContextTypeIs on it, and move on.

Read more comments on GitHub >

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

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