Add TypeScript To An Existing React Project
Learn how to add TypeScript to your existing React project in a few simple steps.
Need to manually type a JSON file?
I learned today that you can add a .d.json.ts
file to manually assign a type to a JSON import.
For example, if you have a data.json
file, you can create a data.d.json.ts
file and define the type inside:
ts
// data.d.json.tsconst data: Record<string, string>;export default data;
You can change the Record<string, string>
type to match the desired shape.
To make TypeScript recognize .d.json.ts
files, you need to add a setting in your tsconfig.json
:
json
{"compilerOptions": {"allowArbitraryExtensions": true}}
This setting lets TypeScript use .d.json.ts
files.
This technique can be particularly useful when working with large .json
files used for test fixtures. If TypeScript infers the type of all the data in the file, it can significantly slow down the TypeScript transpilation process - and your IDE.
By assigning a broader type like Record<string, string>
, TypeScript will use the specified type instead. This optimization can greatly improve the performance of your TypeScript transpilation.
Share this article with your friends
Learn how to add TypeScript to your existing React project in a few simple steps.
Learn the essential TypeScript configuration options and create a concise tsconfig.json file for your projects with this helpful cheatsheet.
Big projects like Svelte and Drizzle are not abandoning TypeScript, despite some recent claims.
Learn different ways to pass a component as a prop in React: passing JSX, using React.ComponentType, and using React.ElementType.
Learn about TypeScript performance and how it affects code type-checking speed, autocomplete, and build times in your editor.
When typing React props in a TypeScript app, using interfaces is recommended, especially when dealing with complex intersections of props.