Skip to content

Constructor parameters of BigInt which is lost BigInt, String, Boolean, Number type #57283

@NWYLZW

Description

@NWYLZW

⚙ Compilation target

ES2020

⚙ Library

TypeScript

Missing / Incorrect Definition

interface BigIntConstructor {
    // wrong
    (value: bigint | boolean | number | string): bigint;
    // right
    (value: bigint | BigInt | boolean | Boolean | number | Number | string | String): bigint;
}

Sample Code

declare const n: unknown

if (n instanceof BigInt) {
  BigInt(n)
  //     ^? TS2345: Argument of type BigInt is not assignable to parameter of type string | number | bigint | boolean
}

// ----

function foo(a: BigInt) {
  BigInt(a)
  //     ^? TS2345: Argument of type BigInt is not assignable to parameter of type string | number | bigint | boolean
}
foo(Object(BigInt(1)))

Documentation Link

We can see in 21.2.1.1 BigInt ( value ) that 2. Let prim be ? ToPrimitive(value, NUMBER). corresponds to our situation and triggers ToPrimitive.
ToPrimitive attempts to retrieve the valueOf method of the Object. For BigInt, Number, String, and Boolean, this method exists, allowing us to obtain a corresponding primitive value. Then, based on the preferredType of NUMBER, it is further converted to a number.
Our problem is essentially answered here, indicating that we should support Boxed Types as input.

image

about

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions