I was looking exactly for this, really good article! I was thinking, what if you use type guards to assert default values in useProvider() function?
exporttypeProviderType=Authentication;exporttypeContextType=ProviderType|undefined;.../* assert type */functionisProviderType(value:ContextType):valueisProviderType{returnvalue!==undefined;}exportfunctionuserProvider(){constvalue=React.useContext<ContextType>(ProviderContext);if(!isProviderType(value))thrownewError("The component using the ...");returnvalue;}
Frontend engineer, working with the Javascripts and the HTMLs and the CSSs. Hardware tinkerer and music enthusiast. Co-founder and former front end engineer at Sourcelair.
I was looking exactly for this, really good article! I was thinking, what if you use type guards to assert default values in useProvider() function?
Would be this a good approach?
Yeah, that's definitely another approach to solve this problem! Depends on what you prefer, it's equally as valid, as far as I am concerned