Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Typescript to understand JSX default props
See microsoft/TypeScript#24422 This addition allows a Component to use defaultProps without having to declare them as optional, in a nutshell: Before class Before extends Component<{ prop?: string }> { static defaultProps = { prop: "default value" }; render() { // this.props.prop is string|undefined } } const element = <Before />; After class After extends Component<{ prop: string }> { static defaultProps = { prop: "default value" }; render() { // typeof this.props.prop is string } } const element = <After />; The definition isn't perfect, it doesn't quite understand type unions where the type of a single property changes, e.g. { type: "number"; value: number } | { type: "string"; value: string } But this case doesn't break, it just would require you to still provide a property. There might be some more things we can do with LibraryManagedAttributes, it could allow the children property to be correct within Components (always an Array) whilst still allowing components to specify the type of children they accept.
- Loading branch information
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters