Skip to content

Make props with infered default types from .defaultProps optional? #221

Closed
@magicmark

Description

@magicmark

Hi! Apologies if this has already been raised, but when we add default types to a component as such:

// @flow

import React from 'react';

type Props = {
    a: string,
    b: string,
};

const Foo = (props: Props) => <div>Hello World! {props.a} {props.b}</div>;

Foo.defaultProps = {
    a: 'foo',
    b: 'bar',
};

export default Foo;

This implies that any props that are in defaultProps are optional.

However react-docgen still sees them as "required": true:

$ node_modules/.bin/react-docgen src/components/Foo/Foo.js | jq .
{
  "description": "",
  "methods": [],
  "props": {
    "a": {
      "flowType": {
        "name": "string"
      },
      "required": true,
      "description": "",
      "defaultValue": {
        "value": "'foo'",
        "computed": false
      }
    },
    "b": {
      "flowType": {
        "name": "string"
      },
      "required": true,
      "description": "",
      "defaultValue": {
        "value": "'bar'",
        "computed": false
      }
    }
  }
}

(using react-docgen@3.0.0-beta8)

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions