TypeScript: Context.Provider value should not be assignable to undefined in the default case#1958
TypeScript: Context.Provider value should not be assignable to undefined in the default case#1958deluksic wants to merge 2 commits intosolidjs:mainfrom
Conversation
|
…ned in the default case
bc7714a to
af13719
Compare
|
I don't believe this requires adding a generic; regardless it will be a breaking (type) change. You'd need a second |
|
Yes, I considered this as well, just thought this approach was a bit simpler. I'm not super worried about this detail, as long as the user-facing API is improved. Should I create another PR with your suggestion and we compare?
Yeah, but hopefully people have not relied on this, and would actually like to see the error otherwise (given that it appears only when strict enabled). For me it was surprising that doing <Context.Provider value={undefined}>actually reverts to the default value and doesn't literally set undefined. This could be reflected in the types, but I wonder why it is like this in the first place. |
Summary
Context providers should not accept
undefinedby default. Example of previously correct code we'd like to avoid:Assumption is that if you really wanted to allow providing
undefinedyou would have written:This does not change the fact that
useContextreturnsundefinedif no default was provided:When combining Resources with Context, it is very easy to forget to test if a given resource is available before assigning it to the provider value.
How did you test this change?
I wrote an additional test to test the usage patterns.
This is type-level only change.