taehwanno / props-type Goto Github PK
View Code? Open in Web Editor NEWUtility 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
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
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 ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.