DEV Community

Discussion on: Working with React context providers in Typescript

segebee profile image
Segun Abisagbo

Great article.

Wasn't comfortable with the developer time solution as it goes against typescript principles.

First time I'm hearing about Proxy. Thanks for sharing.

I think defining default values for the context type might work

mitchkarajohn profile image
Dimitris Karagiannis Author

Thank you, glad you found the article useful!
To be honest, if you do end up defining the Provider's default value with a proxy that follows the schema/type of a valid value, the type assertion when you use the hook is probably not necessary.
The types between default and valid values should match, and if it's a default value it would simply throw on runtime (due to the proxy default value)