Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: currentPropName.toLowerCase is not a function in sort-prop-types #2212

Closed
dfdeagle47 opened this issue Mar 22, 2019 · 2 comments
Closed

Comments

@dfdeagle47
Copy link

The rule sort-prop-types (with ignoreCase: true) will produce this error when using PropTypes.shape with a number as a key.

Here's an example to reproduce this issue

import React from 'react';
import PropTypes from 'prop-types';

export default function Component({ numberToNameMap }) {
  return <div>{numberToNameMap[0]}</div>;
}

Component.propTypes = {
  numberToNameMap: PropTypes.shape({
    0: PropTypes.string.isRequired,
  }).isRequired,
};

The error occurs at this line https://github.com/yannickcr/eslint-plugin-react/blob/v7.12.4/lib/rules/sort-prop-types.js#L192.

Using a string instead of the number as the key when defining the propTypes avoid this issue

import React from 'react';
import PropTypes from 'prop-types';

export default function Component({ numberToNameMap }) {
  return <div>{numberToNameMap[0]}</div>;
}

Component.propTypes = {
  numberToNameMap: PropTypes.shape({
    '0': PropTypes.string.isRequired,
  }).isRequired,
};

I'm not sure if using a number instead of a string as a key is "idiomatic" React; I didn't see an example of this in the React documentation either way.

@ljharb
Copy link
Member

ljharb commented Mar 22, 2019

It’s not idiomatic JS, but it still should work :-)

@pawelnvk
Copy link

pawelnvk commented Apr 9, 2019

I agree, that object keys shouldn't be declared as numbers. It's something you should avoid, but this error shouldn't happen. I created PR that should fix this issue. Please checkout #2230

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants