Skip to content

React component props: mixing defaultProps and union types #25503

Closed
@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 2.9.2

Search Terms: react component props union types defaultprops

Code

Using the new support for defaultProps in TypeScript 3.0 re. #24422

{
    type Props = { shared: string } & (
        | {
              type: 'foo';
              foo: string;
          }
        | {
              type: 'bar';
              bar: string;
          });
    class Component extends ReactComponent<Props> {
        static defaultProps = {
            shared: 'yay',
        };
    }

    <Component type="foo" />; // Expected error, got none
    // Unexpected error
    // Property 'foo' does not exist on type 'Defaultize<Props, { shared: string; }>'.
    <Component type="foo" foo="1" />;
}

#24422 (comment)

/cc @mhegazy who requested that I open an issue to discuss/track this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: JSX/TSXRelates to the JSX parser and emitterIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions