Skip to content

Commit f4dd480

Browse files
author
Islam Attrash
authored
Merge pull request #19 from Attrash-Islam/master
Related to [propTypes with TypeScript #17]
2 parents 33aea10 + 21ccd51 commit f4dd480

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [Stateless Functional Components](#stateless-functional-components)
1414
* [Stateful Class-based Components](#stateful-class-based-components)
1515
* [Typing DefaultProps](#typing-defaultprops)
16+
* [propTypes in TypeScript](#proptypes-in-typescript)
1617
* [Extracting Prop Types](#extracting-prop-types)
1718
* [Basic Prop Types Examples](#basic-prop-types-examples)
1819
* [Useful React Type Examples](#useful-react-type-examples)
@@ -253,6 +254,27 @@ The problem with this approach that if we need to add another prop in the future
253254

254255
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
255256

257+
258+
## propTypes in TypeScript
259+
260+
`propTypes` may seem unnecessary with TypeScript, especially when building React + Typescript **apps**, but they are still relevant when writing **libraries** which may be used by developers working in Javascript.
261+
262+
```ts
263+
interface IMyComponentProps {
264+
autoHeight: boolean;
265+
secondProp: number;
266+
}
267+
268+
export class MyComponent extends React.Component<IMyComponentProps, {}> {
269+
static propTypes = {
270+
autoHeight: PropTypes.bool,
271+
secondProp: PropTypes.number.isRequired,
272+
};
273+
}
274+
```
275+
276+
[Something to add? File an issue](https://github.com/sw-yx/react-typescript-cheatsheet/issues/new).
277+
256278
## Extracting Prop Types
257279

258280
Instead of defining prop types *inline*, you can declare them separately (useful for reusability or code organization):

0 commit comments

Comments
 (0)