Skip to content

Commit c0d4d73

Browse files
JohnLuck77copybara-github
authored andcommitted
Restrict type of BigInt argument
PiperOrigin-RevId: 324305274
1 parent 20e7fb9 commit c0d4d73

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/com/google/javascript/jscomp/testing/TestExternsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static final String lines(String... lines) {
6161
lines(
6262
"/** ",
6363
" * @constructor",
64-
" * @param {*} arg",
64+
" * @param {bigint|number|string} arg",
6565
" * @return {bigint}",
6666
" */",
6767
"function BigInt(arg) {}",

src/com/google/javascript/rhino/jstype/JSTypeRegistry.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,14 @@ private void initializeBuiltInTypes() {
615615
registerNativeType(JSTypeNative.PROMISE_FUNCTION_TYPE, promiseFunctionType);
616616
registerNativeType(JSTypeNative.PROMISE_TYPE, promiseFunctionType.getInstanceType());
617617

618+
// (bigint,number,string)
619+
JSType bigintNumberString = createUnionType(bigIntType, numberType, stringType);
620+
registerNativeType(BIGINT_NUMBER_STRING, bigintNumberString);
621+
618622
// BigInt
619623
FunctionType bigIntObjectFunctionType =
620624
nativeConstructorBuilder("BigInt")
621-
.withParameters(createParameters(allType))
625+
.withParameters(createParameters(bigintNumberString))
622626
.withReturnType(bigIntType)
623627
.build();
624628
bigIntObjectFunctionType.getPrototype(); // Force initialization
@@ -742,10 +746,6 @@ private void initializeBuiltInTypes() {
742746
JSType bigintNumberObject = createUnionType(bigIntObjectType, numberObjectType);
743747
registerNativeType(BIGINT_NUMBER_OBJECT, bigintNumberObject);
744748

745-
// (bigint,number,string)
746-
JSType bigintNumberString = createUnionType(bigIntType, numberType, stringType);
747-
registerNativeType(BIGINT_NUMBER_STRING, bigintNumberString);
748-
749749
// (Bigint,Number,String)
750750
JSType bigintNumberStringObject =
751751
createUnionType(bigIntObjectType, numberObjectType, stringObjectType);

test/com/google/javascript/jscomp/TypeCheckTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23728,6 +23728,17 @@ public void testStrictNumericOperators10() {
2372823728
"required: number"));
2372923729
}
2373023730

23731+
@Test
23732+
public void testBigIntArgument() {
23733+
testTypes("BigInt(1)");
23734+
testTypes(
23735+
"BigInt({})",
23736+
lines(
23737+
"actual parameter 1 of BigInt does not match formal parameter",
23738+
"found : {}",
23739+
"required: (bigint|number|string)"));
23740+
}
23741+
2373123742
@Test
2373223743
public void testBigIntOperators_increment() {
2373323744
testTypes("const x = 1n; x++;");

0 commit comments

Comments
 (0)