JSX | Typescript
Basic usage#
In order to use JSX you must do two things.
- Name your files with a
.tsxextension - Enable the
jsxoption
TypeScript ships with three JSX modes: preserve, react, and react-native.
These modes only affect the emit stage - type checking is unaffected.
The preserve mode will keep the JSX as part of the output to be further consumed by another transform step (e.g. Babel).
Additionally the output will have a .jsx file extension.
The react mode will emit React.createElement, does not need to go through a JSX transformation before use, and the output will have a .js file extension.
The react-native mode is the equivalent of preserve in that it keeps all JSX, but the output will instead have a .js file extension.
| Mode | Input | Output | Output File Extension |
|---|---|---|---|
preserve | <div /> | <div /> | .jsx |
react | <div /> | React.createElement("div") | .js |
react-native | <div /> | <div /> | .js |
You can specify this mode using either the --jsx command line flag or the corresponding option in your tsconfig.json file.
*Note: You can specify the JSX factory function to use when targeting react JSX emit with
--jsxFactoryoption (defaults toReact.createElement)
The as operator#
Type Checking#
Intrinsic elements#
Intrinsic elements are looked up on the special interface JSX.IntrinsicElements.
By default, if this interface is not specified, then anything goes and intrinsic elements will not be type checked.
However, if this interface is present, then the name of the intrinsic element is looked up as a property on the JSX.IntrinsicElements interface.
Note: You can also specify a catch-all string indexer on
JSX.IntrinsicElementsas follows:
Value-based elements#
Value-based elements are simply looked up by identifiers that are in scope.
There are two ways to define a value-based element:
- Function Component (FC)
- Class Component
Because these two types of value-based elements are indistinguishable from each other in a JSX expression, first TS tries to resolve the expression as a Function Component using overload resolution. If the process succeeds, then TS finishes resolving the expression to its declaration. If the value fails to resolve as a Function Component, TS will then try to resolve it as a class component. If that fails, TS will report an error.
Function Component#
As the name suggests, the component is defined as a JavaScript function where its first argument is a props object.
TS enforces that its return type must be assignable to JSX.Element.
Function overloads:
Class Component#
It is possible to define the type of a class component. However, to do so it is best to understand two new terms: the element class type and the element instance type.
Given <Expr />, the element class type is the type of Expr.
So in the example above, if MyComponent was an ES6 class the class type would be that class's constructor and statics.
If MyComponent was a factory function, the class type would be that function.
Once the class type is established, the instance type is determined by the union of the return types of the class type's construct or call signatures (whichever is present).
The element instance type is interesting because it must be assignable to JSX.ElementClass or it will result in an error.
By default JSX.ElementClass is {}, but it can be augmented to limit the use of JSX to only those types that conform to the proper interface.
Attribute type checking#
For intrinsic elements, it is the type of the property on JSX.IntrinsicElements
Note: If an attribute name is not a valid JS identifier (like a
data-*attribute), it is not considered to be an error if it is not found in the element attributes type.
For value-based elements, it is a bit more complex.
It is determined by the type of a property on the element instance type that was previously determined.
Which property to use is determined by JSX.ElementAttributesProperty.
Children Type Checking#
Similar to how TS uses JSX.ElementAttributesProperty to determine the name of props, TS uses JSX.ElementChildrenAttribute to determine the name of children within those props.
JSX.ElementChildrenAttribute should be declared with a single property.
You can specify the type of children like any other attribute. This will override the default type from, eg the React typings if you use them.
React integration#
To use JSX with React you should use the React typings.
These typings define the JSX namespace appropriately for use with React.
Factory Functions#
The exact factory function used by the jsx: react compiler option is configurable. It may be set using either the jsxFactory command line option, or an inline @jsx comment pragma to set it on a per-file basis. For example, if you set jsxFactory to createElement, <div /> will emit as createElement("div") instead of React.createElement("div").
The comment pragma version may be used like so (in TypeScript 2.8):
emits as:
The factory chosen will also affect where the JSX namespace is looked up (for type checking information) before falling back to the global one. If the factory is defined as React.createElement (the default), the compiler will check for React.JSX before checking for a global JSX. If the factory is defined as h, it will check for h.JSX before a global JSX.