JSX Flashcards
What is JSX?
JSX is an embeddable XML-like syntax. It is meant to be transformed into valid JavaScript.
TypeScript supports embedding, type checking, and compiling JSX directly to JavaScript. JSX is used widely in frameworks like React and it allows HTML/XML-like syntax to be used in JavaScript code.
“Documentation - JSX” (typescriptlang.org). Retrieved June 28, 2023.
What are the necessary steps to use JSX in TypeScript?
To use JSX in TypeScript, you must:
- Name your files with a
.tsx
extension - Enable the
jsx
compiler option in your TypeScript configuration.
“JSX Basic usage” (typescriptlang.org). Retrieved June 28, 2023.
What are the different JSX modes in TypeScript and what are their outputs?
TypeScript supports several JSX modes:
-
preserve
: Keeps the JSX as part of the output for further transformation. The output file will have a.jsx
extension. -
react
: EmitsReact.createElement
. The output doesn’t need further JSX transformation and will have a.js
extension. -
react-native
: Equivalent to preserve, but the output will have a.js
extension. -
react-jsx
: Transforms JSX to'_jsx("div", {}, void 0);'
. The output file has a.js
extension. -
react-jsxdev
: Transforms JSX to'_jsxDEV("div", {}, void 0, false, {...}, this);'
. The output file has a.js
extension.
“JSX Basic usage” (typescriptlang.org). Retrieved June 28, 2023.
How do you specify the JSX mode and JSX factory function in TypeScript?
The JSX mode can be specified using the 'jsx'
command line flag or the corresponding 'jsx'
compiler option in your tsconfig.json file.
To specify the JSX factory function (which defaults to React.createElement
when targeting react
JSX emit), you can use the 'jsxFactory'
compiler option in your TypeScript configuration.
“JSX Basic usage” (typescriptlang.org). Retrieved June 28, 2023.
Why does TypeScript disallow angle bracket type assertions in .tsx
files?
TypeScript disallows angle bracket type assertions in .tsx
files because TypeScript also uses angle brackets for type assertions, which would introduce certain parsing difficulties when combined with JSX’s syntax.
“JSX, The as operator” (typescriptlang.org). Retrieved June 28, 2023.
What is the alternative to angle bracket type assertions in .tsx
files?
The as
operator.
For instance,
const foo = bar as foo;
asserts that the variable bar
is of type foo
. The as
operator is identical in behavior to the angle-bracket type assertion style.
“JSX, The as operator” (typescriptlang.org). Retrieved June 28, 2023.
What is the difference between intrinsic elements and value-based elements in JSX type checking?
In JSX type checking, intrinsic elements refer to elements that are intrinsic to the environment, such as a div
or span
in a DOM environment. On the other hand, value-based elements refer to custom components created by the user.
Intrinsic elements are emitted as strings in React (React.createElement("div")
), while value-based elements (components created by the user) are not (React.createElement(MyComponent)
). Furthermore, the types of the attributes being passed in the JSX element are looked up differently for each. Attributes for intrinsic elements should be known intrinsically, whereas components may want to specify their own set of attributes.
In TypeScript and React, an intrinsic element always begins with a lowercase letter, while a value-based element always begins with an uppercase letter.
“JSX Type Checking” Retrieved July 3, 2023.
How does TypeScript distinguish between intrinsic and value-based elements in JSX?
TypeScript uses the same convention that React does for distinguishing between intrinsic and value-based elements in JSX. An intrinsic element always begins with a lowercase letter, while a value-based element (a custom component) always begins with an uppercase letter.
“JSX Type Checking” Retrieved July 3, 2023.
What are intrinsic elements in TypeScript JSX?
Intrinsic elements in TypeScript JSX refer to elements that are intrinsic to the environment, such as a div
or span
in a DOM environment. They are looked up on the special interface JSX.IntrinsicElements
.
“JSX Intrinsic elements” (typescriptlang.org). Retrieved July 3, 2023.
How are intrinsic elements type checked in TypeScript JSX?
Intrinsic elements in TypeScript JSX are type checked using the JSX.IntrinsicElements
interface. If this interface is present, the name of the intrinsic element is looked up as a property on the JSX.IntrinsicElements
interface. For example, if the interface declares a foo
property, <foo />
will pass type checking but <bar />
will fail as it’s not declared in the interface.
For example:
declare namespace JSX { interface IntrinsicElements { foo: any; } } <foo />; // ok <bar />; // error
“JSX Intrinsic elements” (typescriptlang.org). Retrieved July 3, 2023.
What happens if the JSX.IntrinsicElements
interface is not specified in TypeScript JSX?
If the JSX.IntrinsicElements
interface is not specified in TypeScript JSX, then intrinsic elements will not be type checked. This means any intrinsic element name can be used without causing an error.
“JSX Intrinsic elements” (typescriptlang.org). Retrieved July 3, 2023.
How can a catch-all string indexer be used in JSX.IntrinsicElements
interface in TypeScript JSX?
A catch-all string indexer can be used on the JSX.IntrinsicElements
interface to accept any string as an element name. This is done as follows:
declare namespace JSX { interface IntrinsicElements { [elemName: string]: any; } }
“JSX Intrinsic elements” (typescriptlang.org). Retrieved July 3, 2023.
What are value-based elements in TypeScript JSX?
Value-based elements in TypeScript JSX refer to custom components that you’ve created. These elements are looked up by identifiers that are in scope.
For example, if you import and use MyComponent
from "./myComponent"
, <MyComponent />
will be ok. But using an undefined component like <SomeOtherComponent />
will result in an error.
“JSX Value-based elements” (typescriptlang.org). Retrieved July 3, 2023.
How does TypeScript resolve value-based elements in JSX?
In TypeScript, there are two ways to define a value-based element:
- Function Component (FC) and
- Class Component.
In JSX expressions, Function Component and Class Component are indistinguishable from each other. Thus, TypeScript first tries to resolve the expression as a Function Component using overload resolution. If that process succeeds, TypeScript finishes resolving the expression to its declaration.
“JSX Value-based elements” (typescriptlang.org). Retrieved July 3, 2023.
What happens if TypeScript fails to resolve a value-based element as a Function Component?
If TypeScript fails to resolve a value-based element as a Function Component, it then tries to resolve it as a Class Component. If that also fails, TypeScript will report an error.
“JSX Value-based elements” (typescriptlang.org). Retrieved July 3, 2023.