Skip to content

@types/react 16.4.8 breaks prop-types #28015

@webpepper

Description

@webpepper

Here's an example that works fine with versions prior to 16.4.8

MyComponent.tsx

import * as React from "react";
import * as PropsTypes from "prop-types";

interface IProps {
  hello?: string;
  world?: boolean;
}

export const MyComponent: React.SFC<IProps> = props => <div>Hello World!</div>;

MyComponent.propTypes = {
  hello: PropsTypes.string,
  world: PropsTypes.bool
};

I'm not 100% sure where exactly the issue is coming from but downgrading to 16.4.7 makes everything work.

With 16.4.8 I get this error:

[ts]
Type '{ hello: Requireable<string>; world: Requireable<boolean>; }' is not assignable to type 'ValidationMap<IProps> | undefined'.
  Type '{ hello: Requireable<string>; world: Requireable<boolean>; }' is not assignable to type 'ValidationMap<IProps>'.
    Types of property 'hello' are incompatible.
      Type 'Requireable<string>' is not assignable to type 'Validator<string | undefined>'.
        Types of property '[nominalTypeHack]' are incompatible.
          Type 'string | null | undefined' is not assignable to type 'string | undefined'.
            Type 'null' is not assignable to type 'string | undefined'.

Adding .isRequired to all props makes the error go away, but... why?

MyComponent.propTypes = {
  hello: PropsTypes.string.isRequired,
  world: PropsTypes.bool.isRequired
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions