You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently while defining defaultProps we define our component something like this:
interfaceABCProps{requiredProp: number;notRequiredProp: number;// Need to remember not to add ?}classABCextendsReact.PureComponent<ABCProps>{staticdefaultProps={notRequiredProp: 1}render(){// By not addiing ? in Props we don't need to check for undefined.const{ notRequiredProp }=this.propsreturn(<div>{notRequiredProp+1}</div>)}}
But if we need to use ABCProps somewhere else, maybe as a part of parent component or if we create an object of type ABCProps, we will need to define the notRequiredProp as well.
consta: ABCProps={requiredProp: 1,notRequiredProp: 2// This is needed.}
We can have two options:
Define ABCProps' optional props with ? and have component instances this.props be derived from ABCProps and defaultProps. (Breaking change)
interfaceABCProps{requiredProp: number;notRequiredProp?: number;// ? added}classABCextendsReact.PureComponent<ABCProps>{staticdefaultProps={notRequiredProp: 1}render(){// this.props here !== ABCProps as is// typeof this.props === {// [k in keyof ABCProps]: ABC.defaultProps[k] ? NonUndefined<ABCProps[k]> : ABCProps[k]// }const{ notRequiredProp }=this.propsreturn(<div>{notRequiredProp+1}</div>)}}
Define ABCProps' optional props without ? but have ABC["props"] be a modified version of ABCProps such that it makes the keys present in defaultProps as optional.
interfaceABCProps{requiredProp: number;notRequiredProp: number;// Need to remember not to add ?}classABCextendsReact.PureComponent<ABCProps>{staticdefaultProps={notRequiredProp: 1}}// typeof ABC["props"] === {// [k in keyof ABCProps]: ABC.defaultProps[k] ? NonUndefined<ABCProps[k]> : ABCProps[k]// }// typeof ABC["props"] !== ABCProps as isconsta: ABC["props"]={requiredProp: 1}
Use Cases & Examples
Should be able to reuse a Component's Props without having to define optional props as well.
If you're looking for a way to name your component's external props interface without repeating yourself, you want to do something like:
importReactfrom"react";interfaceABCPropsInternal{// Shape of the props used within the componentrequiredProp: number;notRequiredProp: number;}classABCextendsReact.PureComponent<ABCPropsInternal>{staticdefaultProps={notRequiredProp: 1}render(){// By not addiing ? in Props we don't need to check for undefined.const{ notRequiredProp }=this.propsreturn(<div>{ notRequiredProp +1}</div>)}}typeABCProps=JSX.LibraryManagedAttributes<typeofABC,ABCPropsInternal>;// Calculates external props shapeconstprops: ABCProps={requiredProp: 0};constcomponent=<ABC{...props}/>;
Search Terms
defaultProps, react, jsx
Suggestion
Currently while defining defaultProps we define our component something like this:
But if we need to use
ABCProps
somewhere else, maybe as a part of parent component or if we create an object of typeABCProps
, we will need to define thenotRequiredProp
as well.We can have two options:
?
and have component instancesthis.props
be derived fromABCProps
anddefaultProps
. (Breaking change)?
but haveABC["props"]
be a modified version ofABCProps
such that it makes the keys present indefaultProps
as optional.Use Cases & Examples
Should be able to reuse a Component's Props without having to define optional props as well.
Checklist
My first option would be a breaking change as people have started following advice from https://github.com/sw-yx/react-typescript-cheatsheet#typing-defaultprops.
My suggestion meets these guidelines:
Maybe related: #28954
cc: @Kovensky because you are awesome.
The text was updated successfully, but these errors were encountered: