Troubleshooting Handbook: Types
โ ๏ธ 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 and Type Guarding
Union types are handy for solving some of these typing problems:
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:
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.
Optional Types
If a component has an optional prop, add a question mark and assign during destructure (or use defaultProps).
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 Types
Enums in TypeScript default to numbers. You will usually want to use them as strings instead:
Usage:
A simpler alternative to enum is just declaring a bunch of strings with union:
This is handy because TypeScript will throw errors when you mistype a string for your 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.
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:
Of course, try to actually handle the null case instead of asserting :)
Simulating Nominal Types
TS' structural typing is handy, until it is inconvenient. However you can simulate nominal typing with type branding
:
We can create these values with the Companion Object Pattern:
Now TypeScript will disallow you from using the wrong ID in the wrong place:
In future you can use the unique
keyword to brand. See this PR.
Intersection Types
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
:
You can also use Intersection Types to make reusable subsets of props for similar components:
View in the TypeScript Playground
Make sure not to confuse Intersection Types (which are and operations) with Union Types (which are or operations).
Union Types
This 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 Types
Specifically 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:
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:
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
:
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 see readily see examples of overloads in DOM APIs, e.g. createElement
.
Read more about Overloading in the Handbook.
Using Inferred Types
Leaning 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:
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:
Minor caveats on using Partial
Note that there are some TS users who don't agree with using Partial
as it behaves today. See subtle pitfalls of the above example here, and check out this long discussion on why @types/react uses Pick instead of Partial.
The Types I need weren't exported!
This can be annoying but here are ways to grab the types!
- Grabbing the Prop types of a component: Use
React.ComponentProps
andtypeof
, and optionallyOmit
any overlapping types
You may also use ComponentPropsWithoutRef
(instead of ComponentProps) and ComponentPropsWithRef
(if your component specifically forwards refs)
- Grabbing the return type of a function: use
ReturnType
:
In fact you can grab virtually anything public: see this blogpost from Ivan Koshelev
- TS also ships with a
Parameters
utility type for extracting the parameters of a function - for anything more "custom", the
infer
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 oninfer
.
The Types I need don't exist!
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.
any
on everything
Slapping 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:
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 types
You 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.
There are other automated JS to TS conversion tools and migration strategies - see our MIGRATION cheatsheet.
Typing Exported Hooks
Typing Hooks is just like typing pure functions.
The following steps work under two assumptions:
- You have already created a type declaration file as stated earlier in the section.
- You have access to the source code - specifically the code that directly exports the functions you will be using. In most cases, it would be housed in an
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,
then, your type declaration should most likely follow the following syntax.
For instance, the useDarkMode hook exports the functions that follows a similar structure.
As the comments suggest, exporting these config props and return props following the aforementioned structure will result in the following type export.
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.
For more information on creating type definitions for class components, you can refer to this post for reference.