Skip to content

Commit

Permalink
[New] add PropTypes.bigint
Browse files Browse the repository at this point in the history
Closes #355
  • Loading branch information
ljharb committed Dec 22, 2021
1 parent a921554 commit aa90f53
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ MyComponent.propTypes = {
// You can declare that a prop is a specific JS primitive. By default, these
// are all optional.
optionalArray: PropTypes.array,
optionalBigInt: PropTypes.bigint,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
Expand Down
25 changes: 24 additions & 1 deletion __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ describe('PropTypesDevelopmentReact15', () => {

it('should not warn for valid values', () => {
typeCheckPass(PropTypes.array, []);
if (typeof BigInt === 'function') {
typeCheckPass(PropTypes.bigint, BigInt(0));
}
typeCheckPass(PropTypes.bool, false);
typeCheckPass(PropTypes.func, function() {});
typeCheckPass(PropTypes.number, 0);
Expand All @@ -348,6 +351,7 @@ describe('PropTypesDevelopmentReact15', () => {
typeCheckFailRequiredValues(PropTypes.array.isRequired);
typeCheckFailRequiredValues(PropTypes.symbol.isRequired);
typeCheckFailRequiredValues(PropTypes.number.isRequired);
typeCheckFailRequiredValues(PropTypes.bigint.isRequired);
typeCheckFailRequiredValues(PropTypes.bool.isRequired);
typeCheckFailRequiredValues(PropTypes.func.isRequired);
typeCheckFailRequiredValues(PropTypes.shape({}).isRequired);
Expand All @@ -361,6 +365,15 @@ describe('PropTypesDevelopmentReact15', () => {
expectWarningInDevelopment(PropTypes.array.isRequired, []);
expectWarningInDevelopment(PropTypes.array.isRequired, null);
expectWarningInDevelopment(PropTypes.array.isRequired, undefined);
expectWarningInDevelopment(PropTypes.bigint, function() {});
expectWarningInDevelopment(PropTypes.bigint, 42);
if (typeof BigInt === 'function') {
expectWarningInDevelopment(PropTypes.bigint, BigInt(42));
}
expectWarningInDevelopment(PropTypes.bigint.isRequired, function() {});
expectWarningInDevelopment(PropTypes.bigint.isRequired, 42);
expectWarningInDevelopment(PropTypes.bigint.isRequired, null);
expectWarningInDevelopment(PropTypes.bigint.isRequired, undefined);
expectWarningInDevelopment(PropTypes.bool, []);
expectWarningInDevelopment(PropTypes.bool, true);
expectWarningInDevelopment(PropTypes.bool.isRequired, []);
Expand Down Expand Up @@ -436,6 +449,9 @@ describe('PropTypesDevelopmentReact15', () => {

it('should support the arrayOf propTypes', () => {
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
if (typeof BigInt === 'function') {
typeCheckPass(PropTypes.arrayOf(PropTypes.bigint), [BigInt(1), BigInt(2), BigInt(3)]);
}
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
typeCheckPass(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']);
typeCheckPass(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]);
Expand Down Expand Up @@ -539,7 +555,6 @@ describe('PropTypesDevelopmentReact15', () => {
});

describe('Component Type', () => {

it('should support components', () => {
typeCheckPass(PropTypes.element, <div />);
});
Expand All @@ -557,6 +572,14 @@ describe('PropTypesDevelopmentReact15', () => {
'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
if (typeof BigInt === 'function') {
typeCheckFail(
PropTypes.element,
BigInt(123),
'Invalid prop `testProp` of type `bigint` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
}
typeCheckFail(
PropTypes.element,
'foo',
Expand Down
25 changes: 24 additions & 1 deletion __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ describe('PropTypesDevelopmentStandalone', () => {

it('should not warn for valid values', () => {
typeCheckPass(PropTypes.array, []);
if (typeof BigInt === 'function') {
typeCheckPass(PropTypes.bigint, BigInt(0));
}
typeCheckPass(PropTypes.bool, false);
typeCheckPass(PropTypes.func, function() {});
typeCheckPass(PropTypes.number, 0);
Expand All @@ -345,6 +348,7 @@ describe('PropTypesDevelopmentStandalone', () => {
typeCheckFailRequiredValues(PropTypes.array.isRequired);
typeCheckFailRequiredValues(PropTypes.symbol.isRequired);
typeCheckFailRequiredValues(PropTypes.number.isRequired);
typeCheckFailRequiredValues(PropTypes.bigint.isRequired);
typeCheckFailRequiredValues(PropTypes.bool.isRequired);
typeCheckFailRequiredValues(PropTypes.func.isRequired);
typeCheckFailRequiredValues(PropTypes.shape({}).isRequired);
Expand All @@ -358,6 +362,15 @@ describe('PropTypesDevelopmentStandalone', () => {
expectThrowsInDevelopment(PropTypes.array.isRequired, []);
expectThrowsInDevelopment(PropTypes.array.isRequired, null);
expectThrowsInDevelopment(PropTypes.array.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.bigint, function() {});
expectThrowsInDevelopment(PropTypes.bigint, 42);
if (typeof BigInt === 'function') {
expectThrowsInDevelopment(PropTypes.bigint, BigInt(42));
}
expectThrowsInDevelopment(PropTypes.bigint.isRequired, function() {});
expectThrowsInDevelopment(PropTypes.bigint.isRequired, 42);
expectThrowsInDevelopment(PropTypes.bigint.isRequired, null);
expectThrowsInDevelopment(PropTypes.bigint.isRequired, undefined);
expectThrowsInDevelopment(PropTypes.bool, []);
expectThrowsInDevelopment(PropTypes.bool, true);
expectThrowsInDevelopment(PropTypes.bool.isRequired, []);
Expand Down Expand Up @@ -433,6 +446,9 @@ describe('PropTypesDevelopmentStandalone', () => {

it('should support the arrayOf propTypes', () => {
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
if (typeof BigInt === 'function') {
typeCheckPass(PropTypes.arrayOf(PropTypes.bigint), [BigInt(1), BigInt(2), BigInt(3)]);
}
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
typeCheckPass(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']);
typeCheckPass(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]);
Expand Down Expand Up @@ -536,7 +552,6 @@ describe('PropTypesDevelopmentStandalone', () => {
});

describe('Component Type', () => {

it('should support components', () => {
typeCheckPass(PropTypes.element, <div />);
});
Expand All @@ -554,6 +569,14 @@ describe('PropTypesDevelopmentStandalone', () => {
'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
if (typeof BigInt === 'function') {
typeCheckFail(
PropTypes.element,
BigInt(123),
'Invalid prop `testProp` of type `bigint` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
}
typeCheckFail(
PropTypes.element,
'foo',
Expand Down
25 changes: 24 additions & 1 deletion __tests__/PropTypesProductionReact15-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ describe('PropTypesProductionReact15', () => {

it('should not warn for valid values', () => {
expectNoop(PropTypes.array, []);
if (typeof BigInt === 'function') {
expectNoop(PropTypes.bigint, BigInt(0));
}
expectNoop(PropTypes.bool, false);
expectNoop(PropTypes.func, function() {});
expectNoop(PropTypes.number, 0);
Expand All @@ -124,6 +127,7 @@ describe('PropTypesProductionReact15', () => {
expectNoop(PropTypes.array.isRequired);
expectNoop(PropTypes.symbol.isRequired);
expectNoop(PropTypes.number.isRequired);
expectNoop(PropTypes.bigint.isRequired);
expectNoop(PropTypes.bool.isRequired);
expectNoop(PropTypes.func.isRequired);
expectNoop(PropTypes.shape({}).isRequired);
Expand All @@ -137,6 +141,15 @@ describe('PropTypesProductionReact15', () => {
expectNoop(PropTypes.array.isRequired, []);
expectNoop(PropTypes.array.isRequired, null);
expectNoop(PropTypes.array.isRequired, undefined);
expectNoop(PropTypes.bigint, function() {});
expectNoop(PropTypes.bigint, 42);
if (typeof BigInt === 'function') {
expectNoop(PropTypes.bigint, BigInt(42));
}
expectNoop(PropTypes.bigint.isRequired, function() {});
expectNoop(PropTypes.bigint.isRequired, 42);
expectNoop(PropTypes.bigint.isRequired, null);
expectNoop(PropTypes.bigint.isRequired, undefined);
expectNoop(PropTypes.bool, []);
expectNoop(PropTypes.bool, true);
expectNoop(PropTypes.bool.isRequired, []);
Expand Down Expand Up @@ -212,6 +225,9 @@ describe('PropTypesProductionReact15', () => {

it('should support the arrayOf propTypes', () => {
expectNoop(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
if (typeof BigInt === 'function') {
expectNoop(PropTypes.arrayOf(PropTypes.bigint), [BigInt(1), BigInt(2), BigInt(3)]);
}
expectNoop(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
expectNoop(PropTypes.arrayOf(PropTypes.oneOf(['a', 'b'])), ['a', 'b']);
expectNoop(PropTypes.arrayOf(PropTypes.symbol), [Symbol(), Symbol()]);
Expand Down Expand Up @@ -315,7 +331,6 @@ describe('PropTypesProductionReact15', () => {
});

describe('Component Type', () => {

it('should support components', () => {
expectNoop(PropTypes.element, <div />);
});
Expand All @@ -333,6 +348,14 @@ describe('PropTypesProductionReact15', () => {
'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
if (typeof BigInt === 'function') {
expectNoop(
PropTypes.element,
BigInt(123),
'Invalid prop `testProp` of type `bigint` supplied to `testComponent`, ' +
'expected a single ReactElement.',
);
}
expectNoop(
PropTypes.element,
'foo',
Expand Down
12 changes: 12 additions & 0 deletions __tests__/PropTypesProductionStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ describe('PropTypesProductionStandalone', function() {
expectThrowsInProduction(PropTypes.array.isRequired, /please/);
expectThrowsInProduction(PropTypes.array.isRequired, null);
expectThrowsInProduction(PropTypes.array.isRequired, undefined);
expectThrowsInProduction(PropTypes.bigint, function() {});
expectThrowsInProduction(PropTypes.bigint, 42);
if (typeof BigInt === 'function') {
expectThrowsInProduction(PropTypes.bigint, BigInt(42));
}
expectThrowsInProduction(PropTypes.bigint.isRequired, function() {});
expectThrowsInProduction(PropTypes.bigint.isRequired, 42);
expectThrowsInProduction(PropTypes.bigint.isRequired, null);
expectThrowsInProduction(PropTypes.bigint.isRequired, undefined);
expectThrowsInProduction(PropTypes.bool, []);
expectThrowsInProduction(PropTypes.bool.isRequired, []);
expectThrowsInProduction(PropTypes.bool.isRequired, null);
Expand Down Expand Up @@ -140,6 +149,9 @@ describe('PropTypesProductionStandalone', function() {
it('should be a no-op', function() {
expectThrowsInProduction(PropTypes.element, [<div />, <div />]);
expectThrowsInProduction(PropTypes.element, 123);
if (typeof BigInt === 'function') {
expectThrowsInProduction(PropTypes.element, BigInt(123));
}
expectThrowsInProduction(PropTypes.element, 'foo');
expectThrowsInProduction(PropTypes.element, false);
expectThrowsInProduction(PropTypes.element.isRequired, null);
Expand Down
1 change: 1 addition & 0 deletions factoryWithThrowingShims.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function() {
// Keep this list in sync with production version in `./factoryWithTypeCheckers.js`.
var ReactPropTypes = {
array: shim,
bigint: shim,
bool: shim,
func: shim,
number: shim,
Expand Down
1 change: 1 addition & 0 deletions factoryWithTypeCheckers.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
// Keep this list in sync with production version in `./factoryWithThrowingShims.js`.
var ReactPropTypes = {
array: createPrimitiveTypeChecker('array'),
bigint: createPrimitiveTypeChecker('bigint'),
bool: createPrimitiveTypeChecker('boolean'),
func: createPrimitiveTypeChecker('function'),
number: createPrimitiveTypeChecker('number'),
Expand Down

0 comments on commit aa90f53

Please sign in to comment.