React Typescript Cheat Sheet



The one-page guide to TypeScript: usage, examples, links, snippets, and more. This is Devhints.io cheatsheets — a collection of cheatsheets I've written. TypeScript is just like ES2015 with type-checking. All ES2015 (classes, etc) should work. Download InfoWorld’s ultimate R data.table cheat sheet. Author of the Neutralino JavaScript/TypeScript. React Native, and NativeScript. But at Oracle, which oversees development of the. TypeScript React Cheat Sheet TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. This is a list of TypeScript types generated from the declaration files for react, react-dom, react-native and other libraries in https://github.com/DefinitelyTyped/DefinitelyTyped/tree/1349b64. React TypeScript Cheatsheets. Cheatsheets for experienced React developers getting started with TypeScript.

Ever since I started using TypeScript, I can't stop using it. Sometimes finding the correct type and where you should import it from can be a real headache. Especially when building a ReactJS application. This blog post is a great chance to publicly document my most used React TypeScript types. I focus on functional components and react hooks.
The structure of the article is that each paragraph is a standalone tip.

To create a React TypeScript project, we can use Create React App:

There have been lots of talks about the right way to import React. This is the most updated way to do it:

React Ts

The return type of a functional component is ReactElement

If you want to extend the props of a native HTML element, you can use the generic class HTMLAttributes. Let's say I want to create a new button:

Note that we use destructuring to forward the props to the button element.

The children prop is of type ReactNode.

React's events system uses its own types and not the native HTML events. Make sure to import the event from the react library. import { MouseEvent } from 'react'.

React Typescript Cheat SheetTypescript

Pass the correct type to the useRef generic. If you want to create a ref to an input element:

The ref.current type will automatically be HTMLInputElement.

React Typescript Cheat Sheet

The same goes for useState.

If you provide an initial value in both cases, the type will be inferred implicitly.

React Typescript Cheatsheet

When creating custom hooks make sure to explicitly set the returns type. Otherwise, TypeScript may infer incorrectly the types.

Jsdoc Typescript Cheat Sheet

This is far from being a complete cheat sheet but rather documents the things I use the most. Check out this awesome cheat sheet for more information: https://github.com/typescript-cheatsheets/react.

React Typescript Cheat Sheet Download