|
16 | 16 | - [Section 1: Reusable Components/Type Utilities](#section-1-reusable-componentstype-utilities)
|
17 | 17 | * [Higher Order Components](#higher-order-components-hocs)
|
18 | 18 | * [Render Props](#render-props)
|
| 19 | + * [`as` props (passing a component to be rendered)](#as-props-passing-a-component-to-be-rendered) |
19 | 20 | * [Types for Conditional Rendering](#types-for-conditional-rendering)
|
20 | 21 | * [Props: One or the Other but not Both](#props-one-or-the-other-but-not-both)
|
21 | 22 | * [Props: Must Pass Both](#props-one-or-the-other-but-not-both)
|
|
36 | 37 | * [Prettier + TSLint](#prettier--tslint)
|
37 | 38 | * [ESLint + TSLint](#eslint--tslint)
|
38 | 39 | * [Working with Non-TypeScript Libraries (writing your own index.d.ts)](#working-with-non-typescript-libraries-writing-your-own-indexdts)
|
| 40 | +- [Section 4: @types/react and @types/react-dom APIs](#section-4-typesreact-and-typesreact-dom-apis) |
39 | 41 | </details>
|
40 | 42 |
|
41 | 43 | # Section 1: Advanced Guides
|
@@ -179,6 +181,19 @@ export interface Props {
|
179 | 181 |
|
180 | 182 | [Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
|
181 | 183 |
|
| 184 | +## `as` props (passing a component to be rendered) |
| 185 | +
|
| 186 | +`ReactType` is pretty useful to cover most types that can be passed to createElement e.g. |
| 187 | +
|
| 188 | +```tsx |
| 189 | +function PassThrough(props: { as: ReactType<any> }) { |
| 190 | + const { as: Component } = props; |
| 191 | + |
| 192 | + return <Component /> |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +[Thanks @eps1lon](https://github.com/sw-yx/react-typescript-cheatsheet/pull/69) for this |
182 | 197 |
|
183 | 198 | ## Typing a Component that Accepts Different Props
|
184 | 199 |
|
@@ -769,6 +784,63 @@ We have more discussion and examples [in our issue here](https://github.com/sw-y
|
769 | 784 |
|
770 | 785 | </details>
|
771 | 786 |
|
| 787 | +# Section 4: @types/react and @types/react-dom APIs |
| 788 | + |
| 789 | +The `@types` typings export both "public" types meant for your use as well as "private" types that are for internal use. |
| 790 | + |
| 791 | +## `@types/react` |
| 792 | + |
| 793 | +[Link to `.d.ts`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts) |
| 794 | + |
| 795 | +**Namespace: React** |
| 796 | + |
| 797 | +Most Commonly Used Interfaces and Types |
| 798 | + |
| 799 | +- `ReactNode` - anything that is renderable *inside* of JSX, this is NOT the same as what can be rendered by a component! |
| 800 | +- `Component` - base class of all class-based components |
| 801 | +- `PureComponent` - base class for all class-based optimized components |
| 802 | +- `FC`, `FunctionComponent` - a complete interface for function components, often used to type external components instead of typing your own |
| 803 | +- `CSSProperties` - used to type style objects |
| 804 | +- all events: used to type event handlers |
| 805 | +- all event handlers: used to type event handlers |
| 806 | +- all consts: `Children`, `Fragment`, ... are all public and reflect the React runtime namespace |
| 807 | + |
| 808 | +Not Commonly Used but Good to know |
| 809 | + |
| 810 | +- `Ref` - used to type `innerRef` |
| 811 | +- `ReactType` - used for higher order components or operations on components |
| 812 | +- `ComponentType` - used for higher order components where you don't specifically deal with the intrinsic components |
| 813 | +- `ReactPortal` - used if you specifically need to type a prop as a portal, otherwise it is part of `ReactNode` |
| 814 | +- `ComponentClass` - a complete interface for the produced constructor function of a class declaration that extends `Component`, often used to type external components instead of typing your own |
| 815 | +- `JSXElementConstructor` - anything that TypeScript considers to be a valid thing that can go into the opening tag of a JSX expression |
| 816 | +- `ComponentProps` - props of a component |
| 817 | +- `ComponentPropsWithRef` - props of a component where if it is a class-based component it will replace the `ref` prop with its own instance type |
| 818 | +- `ComponentPropsWithoutRef` - props of a component without its `ref` prop |
| 819 | +- all methods: `createElement`, `cloneElement`, ... are all public and reflect the React runtime API |
| 820 | + |
| 821 | +[@Ferdaber's note](https://github.com/sw-yx/react-typescript-cheatsheet/pull/69): I discourage the use of most `...Element` types because of how black-boxy `JSX.Element` is. You should almost always assume that anything produced by `React.createElement` is the base type `React.ReactElement`. |
| 822 | + |
| 823 | +**Namespace: JSX** |
| 824 | + |
| 825 | +- `Element` - the type of any JSX expression |
| 826 | +- `LibraryManagedAttributes` - used to resolve static `defaultProps` and `propTypes` with the internal props type of a component |
| 827 | +- `IntrinsicElements` - every possible built-in component that can be typed in as a lowercase tag name in JSX |
| 828 | + |
| 829 | +**Don't use/Internal/Deprecated** |
| 830 | + |
| 831 | +Anything not listed above is considered an internal type and not public. If you're not sure you can check out the source of `@types/react`. The types are annotated accordingly. |
| 832 | + |
| 833 | +- `SFCElement` |
| 834 | +- `SFC` |
| 835 | +- `ComponentState` |
| 836 | +- `LegacyRef` |
| 837 | +- `StatelessComponent` |
| 838 | + |
| 839 | +## `@types/react-dom` |
| 840 | + |
| 841 | +To be written |
| 842 | + |
| 843 | + |
772 | 844 | # My question isn't answered here!
|
773 | 845 |
|
774 | 846 | - [File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
|
|
0 commit comments