From 7680817d68e5b96f3a0d5a728a64fb07a5171f2a Mon Sep 17 00:00:00 2001 From: kaleb0402 Date: Tue, 9 Apr 2019 01:29:36 -0400 Subject: [PATCH] use "reverse" defaultProp type pattern as suggested in https://github.com/sw-yx/react-typescript-cheatsheet/pull/103 --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5080b1c..d63f0ec 100644 --- a/README.md +++ b/README.md @@ -432,24 +432,24 @@ const Greet = (props: Props) => { /*...*/ }; Greet.defaultProps = defaultProps +``` -// //////////////// -// class components -// //////////////// -export type Props = { - name: string; -}; +For **Class components**, there are [a couple ways to do it](https://github.com/sw-yx/react-typescript-cheatsheet/pull/103#issuecomment-481061483)(including using the `Pick` utility type) but the recommendation is to "reverse" the props definition: -export class Greet extends React.Component { - render() { - const { name } = this.props; - return
Hello ${name.toUpperCase()}!
; +```tsx +type GreetProps = typeof Greet.defaultProps & { + age: number +} + +class Greet extends React.Component { + static defaultProps = { + name: 'world' } - static defaultProps: Partial = { name: 'world' }; + /*...*/ } // Type-checks! No type assertions needed! -let el = ; +let el = ; ```
Why does React.FC break defaultProps?