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

PropTypes.oneOf relies upon stringification when using Symbols #10

Closed
aweary opened this issue Apr 9, 2017 · 3 comments
Closed

PropTypes.oneOf relies upon stringification when using Symbols #10

aweary opened this issue Apr 9, 2017 · 3 comments

Comments

@aweary
Copy link
Contributor

aweary commented Apr 9, 2017

Original issue: facebook/react#9181

@deadratfink
Copy link

deadratfink commented Feb 21, 2018

Hi, is there any chance that this can be fixed quickly? I still experience this error when passing a Symbol instance to a oneOf PropType. This is in factoryWithTypeCheckers.js -> line 289 ->

return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + propValue + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));

which can be fixed easily by JSON.stringify around propValue:

return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + JSON.stringify(propValue) + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));

STACKTRACE:

  console.log node_modules/check-prop-types/index.js:47
    TypeError: Cannot convert a Symbol value to a string
        at validate (.../node_modules/prop-types/factoryWithTypeCheckers.js:289:93)
        at checkType (.../node_modules/prop-types/factoryWithTypeCheckers.js:198:16)
        at checkPropTypes (.../node_modules/check-prop-types/index.js:37:44)
        at ReactComponentTest.expectPropTypes (...)
        at ReactComponentTest.expectPropTypesOfComponent (...)
        at Object.it (...)
        at Object.asyncFn (.../node_modules/jest-jasmine2/build/jasmine-async.js:68:30)
        at resolve (.../node_modules/jest-jasmine2/build/queueRunner.js:38:12)
        at new Promise (<anonymous>)
        at mapper (.../node_modules/jest-jasmine2/build/queueRunner.js:31:21)
        at Promise.resolve.then.el (.../node_modules/p-map/index.js:46:16)
        at <anonymous>
        at process._tickCallback (internal/process/next_tick.js:188:7)

@ljharb
Copy link
Collaborator

ljharb commented Feb 21, 2018

Using JSON is a bad idea because symbols stringify to null. Wrapping propValue in String() should be sufficient.

@deadratfink
Copy link

deadratfink commented Feb 23, 2018

OK, looks like this depends on the environment running on, I just tried on Node.js 8.x during unit tests where I got

Failed prop type: Invalid prop `env` of value `42` supplied to `App.env`, expected one of ["development","shared","stage","production","test","storybook"].

when I provide Symbol(42) to the env property. As you can see the 42 is printed by JSON.stringify.

Of course, this should work out for the other environments:

  • ... + String(propValue) + ... or
  • ... + (typeof propValue === 'symbol' ? propValue.toString() : propValue) + ...

Anyway, hope this gets fixed soon 😃

jimf added a commit to jimf/prop-types that referenced this issue Oct 2, 2018
- Fixes error due to an attempt to coerce a Symbol to a string
- Improves formatting of the "expected" portion of the generated
  warning, outputting for example `["Symbol(A)","Symbol(B)"]` rather
  than `[null,null]`

Fixes facebook#10
ljharb pushed a commit to jimf/prop-types that referenced this issue Feb 8, 2019
- Fixes error due to an attempt to coerce a Symbol to a string
- Improves formatting of the "expected" portion of the generated
  warning, outputting for example `["Symbol(A)","Symbol(B)"]` rather
  than `[null,null]`

Fixes facebook#10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants