Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

adjust default value for createContext example#67

Open
Kageetai wants to merge 1 commit intomarcusolsson:mainfrom
Kageetai:patch-1
Open

adjust default value for createContext example#67
Kageetai wants to merge 1 commit intomarcusolsson:mainfrom
Kageetai:patch-1

Conversation

@Kageetai
Copy link

passing in undefined here throws the following error:

TS2345: Argument of type 'undefined' is not assignable to parameter of type 'App'.

I propose this type cast instead, even though not the most elegant solution, it avoids the error, as the context is already set in initialization of the React context.

passing in `undefined` here throws the following error:
```
TS2345: Argument of type 'undefined' is not assignable to parameter of type 'App'.
```
I propose this type cast instead, even though not the most elegant solution, it avoids the error, as the context is already set in initialization of the React context.
@Acylation
Copy link

Acylation commented Apr 3, 2023

I came across the same problem and would suggest another change here.
export const AppContext = React.createContext<App | undefined>(undefined); also works, please take it into consideration.

@marcusolsson
Copy link
Owner

If export const AppContext = React.createContext<App | undefined>(undefined); works, I think it's a clearer alternative.

@Kageetai
Copy link
Author

Kageetai commented Apr 8, 2023

Yeah, those suggestions make sense as well, but then when actually using the useApp hook, App can also be undefined (as defined for the hook) and has to be checked for existence. This line then throws a TS error for me.
Reminds me that for my project, I actually also removed the App | undefined from the definition of useApp, as it is implicitly derived.

Therefore with the code as currently here and the suggestions above, it would actually throw even two errors for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants