Git Product home page Git Product logo

prop-types-exact's Introduction

prop-types-exact Version Badge

Build Status dependency status dev dependency status License Downloads

npm badge

For use with React PropTypes. Will error on any prop not explicitly specified.

Usage

import PropTypes from 'prop-types';
import exact from 'prop-types-exact';

function Foo({ foo, bar }) {
  return <div>{foo}{bar}</div>
}
Foo.propTypes = exact({
  foo: PropTypes.string,
  bar: PropTypes.number,
});

<Foo foo="hi" bar={3} /> // no warnings

<Foo foo="hi" bar={3} baz="extra" /> // propTypes warning!

Tests

Simply clone the repo, npm install, and run npm test

prop-types-exact's People

Contributors

jackwilsdon avatar ljharb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

prop-types-exact's Issues

doesn't work in my browser when specialProperty is Symbol

I have react@15 and chrome 59.0.3071.115.
With a little tweak in code, I was able to find out the possible cause.

Firstly I made specialProperty any string, including zeroWidthSpace, works fine.
Then I changed it back to Symbol, the function was not called.

then I attached a debugger to validator..

  propertyOne: PropTypes.string,
  propertyTwo: () => {
    debugger
    return null
  },

I was able to reach this frame
image

and the checkType function
image

It turns out that when doing the for... in, the symbol would not be found as a key.
image
image

Allow some props (without using them)

Some HOC does provide multiple props, when sometimes you do not want all of them.

Example: withRouter from react-router adds match, location and history.

A simple approach might to add them in the prop-types, but no-unused-prop-types will throw in this case.

Imagine the following component:

function SomeComponent({ history }) {
  return <button onClick={() => history.push('/')}>go somewhere</button>;
}
SomeComponent.propTypes = exact({
  history: PropTypes.object.isRequired,
});

export default withRouter(SomeComponent);

Actually I would have to do this:

function SomeComponent({ history }) {
  return <button onClick={() => history.push('/')}>go somewhere</button>;
}
SomeComponent.propTypes = exact({
  history: PropTypes.object.isRequired,
  match: PropTypes.any, // eslint-disable-line
  location: PropTypes.any, // eslint-disable-line
});

export default withRouter(SomeComponent);

Should the exact() function takes a ignore list parameter like this ?

function SomeComponent({ history }) {
  return <button onClick={() => history.push('/')}>go somewhere</button>;
}
SomeComponent.propTypes = exact({
  history: PropTypes.object.isRequired,
}, ['match', 'location']);

export default withRouter(SomeComponent);

TODO: be a noop in production

Just like in airbnb-prop-types, this package should noop in production, but should provide the functionality unconditionally via a separate entry point.

Add types for Typescript

I was trying to use the library in a Typescript project but when doing the import it throws the following error:

Could not find a declaration file for module 'prop-types-exact'. '/Users/victorcasas/Documents/projects/mkt/node_modules/prop-types-exact/build/index.js' implicitly has an 'any' type.
Try npm i --save-dev @types/prop-types-exact if it exists or add a new declaration (.d.ts) file containing declare module 'prop-types-exact';ts(7016)

I tried to install @types/ following the instructions in the error, but I realized that the library does not exist.

Uniformize wording on readme

The description says that the library will throw an error on non explicitly set props, but in the example, it says it will throw a "warning".

Ambiguity should be removed by using only "error".

Not working with ESLint plugin `eslint-plugin-react`

Is there any way to make it work together with the ESLint plugin eslint-plugin-react or is there any ESLint plugin which I'm missing?

The rules which I'm currently using and aren't compatible with the usage of prop-types-exact are no-unused-prop-types and prop-types

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.