Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Support JavaScript standard type ‘BigInt’ #355

Closed
@bignose-debian

Description

@bignose-debian

The JavaScript standard data type ‘BigInt’ does not have obvious support in PropTypes.

Existing behaviour with Number

React.version  // → "17.0.2"

Foo = function(props) {
    return React.createElement()
}

props = {tally: 100}
typeof(props.tally)  // → "number"
PropTypes.number  // → function checkType()
typeSpecs = {tally: PropTypes.number}
PropTypes.checkPropTypes(typeSpecs, props)  // → undefined (success)

Foo.propTypes = {
    tally: PropTypes.number,
}
React.createElement(Foo, props)  // → Object { "$$typeof": Symbol(react.element), …}, success

Expected behaviour with BigInt

Foo = function(props) {
    return React.createElement()
}

props = {tally: BigInt(100)}
typeof(props.tally)  // → "bigint"
PropTypes.bigint  // → function checkType()
typeSpecs = {tally: PropTypes.bigint}
PropTypes.checkPropTypes(typeSpecs, props)  // → undefined (success)

Foo.propTypes = {
    tally: PropTypes.instanceOf(BigInt),
}
React.createElement(Foo, props)  // → Object { "$$typeof": Symbol(react.element), …}, success

That is, JavaScript reports the type as "bigint", so the expected type specifier is PropTypes.bigint.

Actual behaviour with BigInt

props = {tally: BigInt(100)}
typeof(props.tally)  // → "bigint"
PropTypes.bigint  // → undefined

So while JavaScript reports the type as "bigint", there is no PropTypes.bigint.

Attempting to work around this with PropTypes.instanceOf appears to work:

props = {tally: BigInt(100)}
typeof(props.tally)  // → "bigint"
PropTypes.instanceOf(BigInt)  // → function checkType()
typeSpecs = {tally: PropTypes.instanceOf(BigInt)}
PropTypes.checkPropTypes(typeSpecs, props)  // → undefined (success)

But this fails when React tests a component's props:

React.version  // → "17.0.2"

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 type BigInt supplied to Foo, expected instance of BigInt.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions