Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Typescript to understand JSX default props #1181

Merged
merged 4 commits into from Sep 10, 2018

Commits on Aug 10, 2018

  1. 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.
    Alexendoo committed Aug 10, 2018
    Configuration menu
    Copy the full SHA
    8f9b829 View commit details
    Browse the repository at this point in the history

Commits on Aug 14, 2018

  1. Configuration menu
    Copy the full SHA
    490d509 View commit details
    Browse the repository at this point in the history

Commits on Aug 22, 2018

  1. Configuration menu
    Copy the full SHA
    fc03c9a View commit details
    Browse the repository at this point in the history

Commits on Sep 10, 2018

  1. Configuration menu
    Copy the full SHA
    d1974c7 View commit details
    Browse the repository at this point in the history