Closed bignose-debian closed 2 years ago
instanceOf
will not work for primitives, so indeed you'd want a PropTypes.bigint
.
instanceOf
will not work for primitives
It appears to work with a call to PropTypes.checkPropTypes
though (see the examples in the initial message). The failure comes when React uses PropTypes.
indeed you'd want a
PropTypes.bigint
.
That is the ideal resolution, agreed.
That seems like a bug in checkPropTypes tho - which version of React did you compare it to?
That seems like a bug in checkPropTypes tho - which version of React did you compare it to?
Now updated the examples to show React.version
.
So, that's React 17 - what does React 16 do? This library hasn't been updated for any changes in React 17.
what does React 16 do?
Same behaviour using prop-types 15.7 with React 16:
React.version // → "16.14.0"
Foo = function(props) {
return React.createElement()
}
Foo.propTypes = {
tally: PropTypes.instanceOf(BigInt),
}
props = {tally: BigInt(100)}
React.createElement(Foo, props)
Warning: Failed prop type: Invalid prop
tally
of typeBigInt
supplied toFoo
, expected instance ofBigInt
.
This library hasn't been updated for any changes in React 17.
Oh, which library should we use for PropTypes support in React 17 and later?
There isn’t any other one - it’s just that this one hasn’t been updated for react 17.
Looks like checkPropTypes has a bug.
for the next person,
yourPropName: (props, propName, componentName) => {
if (typeof props[propName] !== 'bigint') {
return new Error(`Invalid prop '${propName}' supplied to '${componentName}', expected 'BigInt'.`);
}
}
If you observe differences in the way checkPropTypes works versus React, please file a bug on the appropriate library - here if checkPropTypes is clearly wrong, and on React itself if React is clearly wrong.
The JavaScript standard data type ‘BigInt’ does not have obvious support in PropTypes.
Existing behaviour with
Number
Expected behaviour with
BigInt
That is, JavaScript reports the type as "bigint", so the expected type specifier is
PropTypes.bigint
.Actual behaviour with
BigInt
So while JavaScript reports the type as "bigint", there is no
PropTypes.bigint
.Attempting to work around this with
PropTypes.instanceOf
appears to work:But this fails when React tests a component's props: