Coder Social home page Coder Social logo

taehwanno / props-type Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 0.0 163 KB

Utility type that defines the type of the React component props through propTypes and defaultProps in TypeScript

Home Page: http://npm.im/props-type

License: MIT License

JavaScript 11.08% TypeScript 88.92%
prop-types react typescript

props-type's People

Contributors

dependabot[bot] avatar taehwanno avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

props-type's Issues

A quick fix for the "Limits" section

In the https://github.com/taehwanno/props-type#limits section of the README.md file you mention the issue with oneOf. I don't think it's inherently a problem of InferProps from the @types/prop-types but simply how TypeScript handles array literals.

A simple fix for this issue is to use as const assertion introduced in TS 3.4, like so:

const propTypes = {
  type: PropTypes.oneOf(['button', 'submit', 'reset'] as const),
};

However, this does not resolve the issue of defaultProps still widening the type. Which again can be solved with simple as const assertion ๐Ÿ˜‰

const defaultProps = {
  type: 'button',
} as const;

There is however an issue when using React.FC type for functional components which I haven't found a solution for yet, except for maybe casting it in the assignment expression:

const Button : React.FC<ButtonProps> = ({ type }) => { /* ... */ }

Button.propTypes = propTypes; // type error
(Button as React.FC<PropTypes.InferProps<typeof propTypes>>).propTypes = propTypes; // not ideal - very explicit but way too verbose
Button.propTypes = propTypes as any; // less verbose, the `Button.propTypes` original type is preserved
Button.defaultProps = defaultProps;

Error:

Type '{ type: Requireable<"button" | "submit" | "reset">; }' is not assignable to type 'WeakValidationMap<WithDefaultProps<InferProps<{ type: Requireable<"button" | "submit" | "reset">; }>, { readonly type: "button"; }>>'.
  Types of property 'type' are incompatible.
    Type 'Requireable<"button" | "submit" | "reset">' is not assignable to type 'Validator<"button" | "submit" | "reset">'.
      Types of property '[nominalTypeHack]' are incompatible.
        Type '{ type: "button" | "submit" | "reset" | null | undefined; } | undefined' is not assignable to type '{ type: "button" | "submit" | "reset"; } | undefined'.
          Type '{ type: "button" | "submit" | "reset" | null | undefined; }' is not assignable to type '{ type: "button" | "submit" | "reset"; }'.
            Types of property 'type' are incompatible.
              Type '"button" | "submit" | "reset" | null | undefined' is not assignable to type '"button" | "submit" | "reset"'.
                Type 'undefined' is not assignable to type '"button" | "submit" | "reset"'.ts(2322)

PS. great job on the package! found it after translating your Medium article ๐Ÿ™‚

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.