⚠️ Have you read the TypeScript FAQ Your answer might be there!
Facing weird type errors? You aren't alone. This is the hardest part of using TypeScript with React. Be patient - you are learning a new language after all. However, the more you get good at this, the less time you'll be working against the compiler and the more the compiler will be working for you!
Try to avoid typing with any
as much as possible to experience the full benefits of TypeScript. Instead, let's try to be familiar with some of the common strategies to solve these issues.
Union types are handy for solving some of these typing problems:
class App extends React.Component<
{},
{
count: number | null;
}
> {
state = {
count: null,
};
render() {
return <div onClick={() => this.increment(1)}>{this.state.count}</div>;
}
increment = (amt: number) => {
this.setState((state) => ({
count: (state.count || 0) + amt,
}));
};
}
View in the TypeScript Playground
Type Guarding: Sometimes Union Types solve a problem in one area but create another downstream. If A
and B
are both object types, A | B
isn't "either A or B", it is "A or B or both at once", which causes some confusion if you expected it to be the former. Learn how to write checks, guards, and assertions (also see the Conditional Rendering section below). For example:
interface Admin {
role: string;
}
interface User {
email: string;
}
function redirect(user: Admin | User) {
if ("role" in user) {
routeToAdminPage(user.role);
} else {
routeToHomePage(user.email);
}
}
function isAdmin(user: Admin | User): user is Admin {
return (user as any).role !== undefined;
}
View in the TypeScript Playground
Method 2 is also known as User-Defined Type Guards and can be really handy for readable code. This is how TS itself refines types with typeof
and instanceof
.
If you need if...else
chains or the switch
statement instead, it should "just work", but look up Discriminated Unions if you need help. (See also: Basarat's writeup). This is handy in typing reducers for useReducer
or Redux.
If a component has an optional prop, add a question mark and assign during destructure (or use defaultProps).
class MyComponent extends React.Component<{
message?: string;
}> {
render() {
const { message = "default" } = this.props;
return <div>{message}</div>;
}
}
You can also use a !
character to assert that something is not undefined, but this is not encouraged.
Something to add? File an issue with your suggestions!
Enum TypesWe recommend avoiding using enums as far as possible.
Enums have a few documented issues (the TS team agrees). A simpler alternative to enums is just declaring a union type of string literals:
export declare type Position = "left" | "right" | "top" | "bottom";
If you must use enums, remember that enums in TypeScript default to numbers. You will usually want to use them as strings instead:
export enum ButtonSizes {
default = "default",
small = "small",
large = "large",
}
export const PrimaryButton = (
props: Props & React.HTMLProps<HTMLButtonElement>
) => <Button size={ButtonSizes.default} {...props} />;
Type Assertion
Sometimes you know better than TypeScript that the type you're using is narrower than it thinks, or union types need to be asserted to a more specific type to work with other APIs, so assert with the as
keyword. This tells the compiler you know better than it does.
class MyComponent extends React.Component<{
message: string;
}> {
render() {
const { message } = this.props;
return (
<Component2 message={message as SpecialMessageType}>{message}</Component2>
);
}
}
View in the TypeScript Playground
Note that you cannot assert your way to anything - basically it is only for refining types. Therefore it is not the same as "casting" a type.
You can also assert a property is non-null, when accessing it:
element.parentNode!.removeChild(element);
myFunction(document.getElementById(dialog.id!)!);
let userID!: string;
Of course, try to actually handle the null case instead of asserting :)
Simulating Nominal TypesTS' structural typing is handy, until it is inconvenient. However you can simulate nominal typing with type branding
:
type OrderID = string & { readonly brand: unique symbol };
type UserID = string & { readonly brand: unique symbol };
type ID = OrderID | UserID;
We can create these values with the Companion Object Pattern:
function OrderID(id: string) {
return id as OrderID;
}
function UserID(id: string) {
return id as UserID;
}
Now TypeScript will disallow you from using the wrong ID in the wrong place:
function queryForUser(id: UserID) {
}
queryForUser(OrderID("foobar"));
In future you can use the unique
keyword to brand. See this PR.
Adding two types together can be handy, for example when your component is supposed to mirror the props of a native component like a button
:
export interface PrimaryButtonProps {
label: string;
}
export const PrimaryButton = (
props: PrimaryButtonProps & React.ButtonHTMLAttributes<HTMLButtonElement>
) => {
return <button {...props}> {props.label} </button>;
};
Playground here
You can also use Intersection Types to make reusable subsets of props for similar components:
type BaseProps = {
className?: string,
style?: React.CSSProperties
name: string
}
type DogProps = {
tailsCount: number
}
type HumanProps = {
handsCount: number
}
export const Human = (props: BaseProps & HumanProps) =>
export const Dog = (props: BaseProps & DogProps) =>
View in the TypeScript Playground
Make sure not to confuse Intersection Types (which are and operations) with Union Types (which are or operations).
Union TypesThis section is yet to be written (please contribute!). Meanwhile, see our commentary on Union Types usecases.
The ADVANCED cheatsheet also has information on Discriminated Union Types, which are helpful when TypeScript doesn't seem to be narrowing your union type as you expect.
Overloading Function TypesSpecifically when it comes to functions, you may need to overload instead of union type. The most common way function types are written uses the shorthand:
type FunctionType1 = (x: string, y: number) => number;
But this doesn't let you do any overloading. If you have the implementation, you can put them after each other with the function keyword:
function pickCard(x: { suit: string; card: number }[]): number;
function pickCard(x: number): { suit: string; card: number };
function pickCard(x): any {
}
However, if you don't have an implementation and are just writing a .d.ts
definition file, this won't help you either. In this case you can forego any shorthand and write them the old-school way. The key thing to remember here is as far as TypeScript is concerned, functions are just callable objects with no key
:
type pickCard = {
(x: { suit: string; card: number }[]): number;
(x: number): { suit: string; card: number };
};
Note that when you implement the actual overloaded function, the implementation will need to declare the combined call signature that you'll be handling, it won't be inferred for you. You can readily see examples of overloads in DOM APIs, e.g. createElement
.
Read more about Overloading in the Handbook.
Using Inferred TypesLeaning on TypeScript's Type Inference is great... until you realize you need a type that was inferred, and have to go back and explicitly declare types/interfaces so you can export them for reuse.
Fortunately, with typeof
, you won't have to do that. Just use it on any value:
const [state, setState] = useState({
foo: 1,
bar: 2,
});
const someMethod = (obj: typeof state) => {
setState(obj);
};
Using Partial Types
Working with slicing state and props is common in React. Again, you don't really have to go and explicitly redefine your types if you use the Partial
generic type:
const [state, setState] = useState({
foo: 1,
bar: 2,
});
const partialStateUpdate = (obj: Partial<typeof state>) =>
setState({ ...state, ...obj });
partialStateUpdate({ foo: 2 });
Minor caveats on using Partial
The Types I need weren't exported!
This can be annoying but here are ways to grab the types!
React.ComponentProps
and typeof
, and optionally Omit
any overlapping typesimport { Button } from "library";
type ButtonProps = React.ComponentProps<typeof Button>;
type AlertButtonProps = Omit<ButtonProps, "onClick">;
const AlertButton = (props: AlertButtonProps) => (
<Button onClick={() => alert("hello")} {...props} />
);
You may also use ComponentPropsWithoutRef
(instead of ComponentProps) and ComponentPropsWithRef
(if your component specifically forwards refs)
ReturnType
:
function foo(bar: string) {
return { baz: 1 };
}
type FooReturn = ReturnType<typeof foo>;
In fact you can grab virtually anything public: see this blogpost from Ivan Koshelev
function foo() {
return {
a: 1,
b: 2,
subInstArr: [
{
c: 3,
d: 4,
},
],
};
}
type InstType = ReturnType<typeof foo>;
type SubInstArr = InstType["subInstArr"];
type SubInstType = SubInstArr[0];
let baz: SubInstType = {
c: 5,
d: 6,
};
type SubInstType2 = ReturnType<typeof foo>["subInstArr"][0];
let baz2: SubInstType2 = {
c: 5,
d: 6,
};
Parameters
utility type for extracting the parameters of a functioninfer
keyword is the basic building block for this, but takes a bit of getting used to. Look at the source code for the above utility types, and this example to get the idea. Basarat also has a good video on infer
.What's more annoying than modules with unexported types? Modules that are untyped!
Before you proceed - make sure you have checked that types don't exist in DefinitelyTyped or TypeSearch
Fret not! There are more than a couple of ways in which you can solve this problem.
Slappingany
on everything
A lazier way would be to create a new type declaration file, say typedec.d.ts
– if you don't already have one. Ensure that the path to file is resolvable by TypeScript by checking the include
array in the tsconfig.json
file at the root of your directory.
Within this file, add the declare
syntax for your desired module, say my-untyped-module
– to the declaration file:
declare module "my-untyped-module";
This one-liner alone is enough if you just need it to work without errors. A even hackier, write-once-and-forget way would be to use "*"
instead which would then apply the Any
type for all existing and future untyped modules.
This solution works well as a workaround if you have less than a couple untyped modules. Anything more, you now have a ticking type-bomb in your hands. The only way of circumventing this problem would be to define the missing types for those untyped modules as explained in the following sections.
Autogenerate typesYou can use TypeScript with --allowJs
and --declaration
to see TypeScript's "best guess" at the types of the library.
If this doesn't work well enough, use dts-gen
to use the runtime shape of the object to accurately enumerate all available properties. This tends to be very accurate, BUT the tool does not yet support scraping JSDoc comments to populate additional types.
npm install -g dts-gen
dts-gen -m <your-module>
There are other automated JS to TS conversion tools and migration strategies - see our MIGRATION cheatsheet.
Typing Exported HooksTyping Hooks is just like typing pure functions.
The following steps work under two assumptions:
index.js
file. Typically you need a minimum of two type declarations (one for Input Prop and the other for Return Prop) to define a hook completely. Suppose the hook you wish to type follows the following structure,
const useUntypedHook = (prop) => {
return {
};
};
export default useUntypedHook;
then, your type declaration should most likely follow the following syntax.
declare module 'use-untyped-hook' {
export interface InputProps { ... }
export interface ReturnProps { ... }
export default function useUntypedHook(
prop: InputProps
): ReturnProps;
}
For instance, the useDarkMode hook exports the functions that follows a similar structure.
const useDarkMode = (
initialValue = false,
{
element,
classNameDark,
classNameLight,
onChange,
storageKey = "darkMode",
storageProvider,
global,
} = {}
) => {
return {
value: state,
enable: useCallback(() => setState(true), [setState]),
disable: useCallback(() => setState(false), [setState]),
toggle: useCallback(() => setState((current) => !current), [setState]),
};
};
export default useDarkMode;
As the comments suggest, exporting these config props and return props following the aforementioned structure will result in the following type export.
declare module "use-dark-mode" {
export interface DarkModeConfig {
classNameDark?: string;
classNameLight?: string;
element?: HTMLElement;
onChange?: (val?: boolean) => void;
storageKey?: string;
storageProvider?: WindowLocalStorage;
global?: Window;
}
export interface DarkMode {
readonly value: boolean;
enable: () => void;
disable: () => void;
toggle: () => void;
}
export default function useDarkMode(
initialState?: boolean,
config?: DarkModeConfig
): DarkMode;
}
Typing Exported Components
In case of typing untyped class components, there's almost no difference in approach except for the fact that after declaring the types, you export the extend the type using class UntypedClassComponent extends React.Component<UntypedClassComponentProps, any> {}
where UntypedClassComponentProps
holds the type declaration.
For instance, sw-yx's Gist on React Router 6 types implemented a similar method for typing the then untyped RR6.
declare module "react-router-dom" {
import * as React from 'react';
type NavigateProps<T> = {
to: string | number,
replace?: boolean,
state?: T
}
export class Navigate<T = any> extends React.Component<NavigateProps<T>>{}
For more information on creating type definitions for class components, you can refer to this post for reference.
Frequent Known Problems with TypeScriptJust a list of stuff that React developers frequently run into, that TS has no solution for. Not necessarily TSX only.
TypeScript doesn't narrow after an object element null checkRef: https://mobile.twitter.com/tannerlinsley/status/1390409931627499523. see also https://github.com/microsoft/TypeScript/issues/9998
TypeScript doesn't let you restrict the type of childrenGuaranteeing typesafety for this kind of API isn't possible:
<Menu>
<MenuItem/> {}
<MenuLink/> {}
<div> {}
</Menu>
Source: https://twitter.com/ryanflorence/status/1085745787982700544?s=20
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4