...

/

Type Safety Problem While Using TypeScript with React

Type Safety Problem While Using TypeScript with React

Learn about the type safety problem of TypeScript and some solutions to it.

The fundamental problem

The core issue on our side is not necessarily a TypeScript problem. It’s a React problem:

  1. When we call React.createContext, TypeScript will want to know what value we can expect.

  2. But we don’t have that value yet because we have to wait for the Provider component to mount to use useState.

  3. TypeScript will consider this an issue because we’re passing it something that isn’t what it expects. ...