npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

props-type

v1.0.1

Published

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

Downloads

2,156

Readme

props-type npm version github actions

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

Table of contents

Installation

# with NPM
$ npm install --save-dev props-type

# with Yarn
$ yarn add --dev props-type

Prerequisite

Usage

import PropsType from 'props-type';

// Without defaultProps
type Props = PropsType<typeof propTypes>;

// With defaultProps
type Props = PropsType<typeof propTypes, typeof defaultProps>;

Type Inference

Optional (without isRequired)

without defaultProps

const propTypes = { disabled: PropTypes.bool };
type Props = PropsType<typeof propTypes>;
  • Internal : Props type is disabled: boolean | null | undefined
  • External : <Button disabled?: boolean | null | undefined />

with defaultProps

const propTypes = { disabled: PropTypes.bool };
const defaultProps = { disabled: false };
type Props = PropsType<typeof propTypes, typeof defaultProps>;
  • Internal : Props type is boolean
  • External : <Button disabled?: boolean | undefined />

Required (with isRequired)

without defaultProps

const propTypes = { disabled: PropTypes.bool.isRequired };
type Props = PropsType<typeof propTypes>;
  • Internal : Props type is disabled: boolean
  • External : <Button disabled: boolean />

with defaultProps

const propTypes = { disabled: PropTypes.bool.isRequired };
const defaultProps = { disabled: false };
type Props = PropsType<typeof propTypes, typeof defaultProps>;
  • Internal : Props type is disabled: boolean
  • External : <Button disabled?: boolean | undefined />

Example

without defaultProps

External

const propTypes = {
  className: PropTypes.string,
  disabled: PropTypes.bool.isRequired,
  onClick: PropTypes.func.isRequired,
  onDoubleClick: PropTypes.func,
};

type ButtonProps = PropsType<typeof propTypes>;
// Correct
<Button disabled onClick={onClick} />
<Button className="primary" disabled onClick={onClick} />
<Button disabled onClick={onClick} onDoubleClick={onDoubleClick} />
<Button className="primary" disabled onClick={onClick} onDoubleClick={onDoubleClick} />

// Invalid
<Button /> // Property 'disabled' and 'onClick' is missing
<Button disabled /> // Property 'onClick' is missing
<Button onClick /> // Property 'disabled' is missing
  • required : disabled, onClick
  • optional : className, onDoubleClick

Internal

function Button({ className, disabled, onClick, onDoubleClick }: ButtonProps) {
  return (
    <button
      className={className}
      disabled={disabled}
      onClick={onClick}
      onDoubleClick={onDoubleClick}
    />
  );
}

Button.propTypes = propTypes;
  • className type : string | null | undefined
  • disabled type : boolean
  • onClick type : ((...args: any[]) => any)
  • onDoubleClick type : ((...args: any[]) => any) | null | undefined

with defaultProps

External

const propTypes = {
  className: PropTypes.string,
  disabled: PropTypes.bool.isRequired,
  onClick: PropTypes.func.isRequired,
  onDoubleClick: PropTypes.func,
};

const defaultProps = {
  className: 'primary',
  onDoubleClick(event: React.MouseEvent<HTMLButtonElement>) {},
};

type ButtonProps = PropsType<typeof propTypes>;
// Correct
<Button disabled onClick={onClick} />
<Button className="secondary" disabled onClick={onClick} />
<Button disabled onClick={onClick} onDoubleClick={(event: React.MouseEvent<HTMLButtonElement>) => {}} />
<Button className="secondary" disabled onClick={onClick} onDoubleClick={(event: React.MouseEvent<HTMLButtonElement>) => {}} />

// Invalid
<Button /> // Property 'disabled' and 'onClick' is missing
<Button disabled /> // Property 'onClick' is missing
<Button onClick={onClick} /> // Property 'disabled' is missing
<Button disabled onClick={onClick} onDoubleClick={(a: number) => {}} /> // Type '(a: number) => void' is not assignable to type '(event: MouseEvent<HTMLButtonElement, MouseEvent>) => void'
  • required : disabled, onClick
  • optional : className, onDoubleClick

Internal

function Button({ className, disabled, onClick, onDoubleClick }: ButtonProps) {
  return (
    <button
      className={className}
      disabled={disabled}
      onClick={onClick}
      onDoubleClick={onDoubleClick}
    />
  );
}

Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
  • className type : string
  • disabled type : boolean
  • onClick type : ((...args: any[]) => any)
  • onDoubleClick type : (event: React.MouseEvent<HTMLButtonElement>) => void

null or undefined in defaultProps

External

const propTypes = {
  className: PropTypes.string,
  testId: PropTypes.string,
};

const defaultProps = {
  className: null,
  testId: undefined,
};

type ButtonProps = PropsType<typeof propTypes, typeof defaultProps>;
// Correct
<Button />
<Button className={null} />
<Button testId={undefined} />

// Incorrect
<Button className={undefined} />
<Button testId={null} />
  • required : N/A
  • optional : className, testId

Internal

function Button({ className, testId }: ButtonProps) {
  return <button className={className} data-testid={testId} />;
}

Button.propTypes = propTypes;
Button.defaultProps = defaultProps;
  • className type : string | null
  • testId type : string | undefined

Limits

The prop type of oneOf in prop-types is not inferenced to union type.

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

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

type ButtonProps = PropsType<typeof propTypes, typeof defaultProps>;

function Button({ type }: ButtonProps) {
  return <button type={type}>Button</button>; // Type 'string | null | undefined' is not assignable to type '"button" | "submit" | "reset" | undefined'.
}

Button.propTypes = propTypes;
Button.defaultProps = defaultProps;

type prop is inferenced to string (not a 'button' | 'submit' | 'reset' union type) because prop-types typescript type declaration currently have problems related to InferProps type (in @types/prop-types). If you want to inference oneOf as union type, this workaround can help you.

type ButtonProps = PropsType<typeof propTypes, typeof defaultProps> & {
  type: 'button' | 'submit' | 'reset';
};

Thanks

This package is inspired by Brie Bunge in Adopting TypeScript at Scale, JSConf Hawaii 2019

License

MIT © Taehwan Noh