From c290af025224e704aa85af8f50025d2758b92246 Mon Sep 17 00:00:00 2001 From: Islam Attrash Date: Mon, 4 Jun 2018 22:22:50 +0300 Subject: [PATCH 1/3] [defaultProps] - Related to "How to handle default props?" #4 --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f780fb4c..26e9d541 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,51 @@ This is not yet written. Please PR or [File an issue](https://github.com/sw-yx/r # Typing DefaultProps -It is easy to [type defaults for functional components](https://twitter.com/GeeWengel/status/1000242363376205825), but there is some debate in the community on how to type the `static defaultProps` field for class-based components. We have an active discussion on several approaches on how to do this. [Please check out our issue here](https://github.com/sw-yx/react-typescript-cheatsheet/issues/4). +It is easy to type a defaultProps static member of a React component. There's more than one way to do it, but since we want to show the neatest code as possible +we choosed to propose this way of implementing them: + +```ts +interface IMyComponentProps { + firstProp: string; + secondProp: IPerson[]; +} + +export class MyComponent extends React.Component { + static defaultProps: Partial = { + firstProp: "default", + }; +} +``` + +
+ +Explanation + +This proposal is using `Partial type` feature in TypeScript, which means that the current interface will fullfill a partial +version on the wrapped interface. In that way we can extend defaultProps without any changes in the types! + +The other suggestions was related to create a new interface that will look like this: + +```ts +interface IMyComponentProps { + firstProp: string; + secondProp: IPerson[]; +} + +interface IMyComponentDefaultProps { + firstProp: string; +} + +export class MyComponent extends React.Component { + static defaultProps: IMyComponentDefaultProps = { + firstProp: "default", + }; +} +``` + +The problem with this approach that if we need to add another prop in the future to the defaultProps map then I should update the +`IMyComponentDefaultProps`! +
# Extracting Prop Types From 7c876bea4f04720a908f83c88c09e2f69eef1e2b Mon Sep 17 00:00:00 2001 From: Islam Attrash Date: Mon, 4 Jun 2018 22:26:27 +0300 Subject: [PATCH 2/3] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index c79f1a32..0eb56c55 100644 --- a/README.md +++ b/README.md @@ -192,8 +192,6 @@ class App extends React.Component<{ ## Typing DefaultProps -It is easy to [type defaults for functional components](https://twitter.com/GeeWengel/status/1000242363376205825), but there is some debate in the community on how to type the `static defaultProps` field for class-based components. - It is easy to type a defaultProps static member of a React component. There's more than one way to do it, but since we want to show the neatest code as possible we choosed to propose this way of implementing them: From d701fad17607f00e01b3e79ac2b43f97bb347009 Mon Sep 17 00:00:00 2001 From: Islam Attrash Date: Mon, 4 Jun 2018 22:29:41 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0eb56c55..370617e4 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,7 @@ export class MyComponent extends React.Component { } ``` -The problem with this approach that if we need to add another prop in the future to the defaultProps map then I should update the +The problem with this approach that if we need to add another prop in the future to the defaultProps map then we should update the `IMyComponentDefaultProps`!