1

When you create a typescript project on Stackblitz it turns out that there is an issue with BigInt

enter image description here

const val = BigInt(10);

It doesn't know BigInt. Although the code runs fine I still want the error to go away :)

So, in the above case I can add the follow

declare function BigInt(inp: number | string);

This works, the error is gone. But when I add more code, like this

declare function BigInt(inp: number | string);

function convert(inp: string): BigInt {
    return BigInt(inp);
}

const val = BigInt(10);

The declare at the top is not enough

enter image description here

It complains that BigInt refers to a value and is being used as a Type.

I'm not sure any more what I can do to fix this. Any suggestions?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

1

BigInt isn't a part of typescript, it is a part of javascript of some browsers, but Safari for example doesn't support it, that means code without a polyfill won't work there.

To define it properly you need to declare its interface, its variable and its constructor.


declare interface BigInt {
  // here all magic you want to declare
  test: number
};

declare var BigInt: BigIntConstructor;

declare interface BigIntConstructor {
    new(value?: any): BigInt;
    (value?: any): BigInt;
    readonly prototype: BigInt;
};



function convert(inp: string): BigInt {
    return BigInt(inp);
}

const val = BigInt(10).test; // an example.
satanTime
  • 12,631
  • 1
  • 25
  • 73