Home / Companies / LogRocket / Blog / Post Details
Content Deep Dive

How to use TypeScript with React 18 alpha

Blog post from LogRocket

Post Details
Company
Date Published
Author
John Reilly
Word Count
789
Language
-
Hacker News Points
-
Summary

React 18 alpha can be used with TypeScript, but it requires specific steps to ensure compatibility due to API changes and TypeScript's type definitions. To create a React app with TypeScript, developers first use Create React App with a TypeScript template and then upgrade to the React 18 alpha version. The new ReactDOM.createRoot API is essential for enabling React 18's features, but TypeScript initially does not recognize it, leading to errors. To resolve this, developers must update type definitions and adjust their tsconfig.json file to include "react/next" and "react-dom/next" in the "types" array. Additionally, developers need to handle potential null values when accessing the root element due to strictNullChecks mode in TypeScript. Once these adjustments are made, a working React 18 application with TypeScript is achievable, allowing developers to enjoy the latest features while ensuring type safety.