Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error TS2318: Cannot find global type 'BigInt' #49101

Closed
h-joo opened this issue May 13, 2022 · 7 comments · Fixed by #49104
Closed

error TS2318: Cannot find global type 'BigInt' #49101

h-joo opened this issue May 13, 2022 · 7 comments · Fixed by #49104
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@h-joo
Copy link
Contributor

h-joo commented May 13, 2022

Bug Report

The provided code fails with compiler error: error TS2318: Cannot find global type 'BigInt'. without any location information.

🔎 Search Terms

BigInt, Object.freeze

🕗 Version & Regression Information

This changed between versions 4.6.4 and 4.7-beta

⏯ Playground Link

No playground reproduce for this bug as it's not possible to set target and lib differently from the playground.

Steps to reproduce

mkdir test_47rc
cd test_47rc
npm init
npm install typescript@rc
echo "Object.freeze({foo() {}});" > index.ts
./node_modules/.bin/tsc --target es2020 --lib es2019 index.ts

Note : ./node_modules/.bin/tsc --target es2020 --lib es2020 index.ts does not print an error.

💻 Code

Object.freeze({foo() {}});

🙁 Actual behavior

TypeScript compiler prints error TS2318: Cannot find global type 'BigInt'. without any source location.

🙂 Expected behavior

I'm not sure what the actual behavior here needs to be, but at least the new additioal type definition of Object.freeze in lib.es5.d.ts start to trigger this failure.

    /**
     * Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
     * @param o Object on which to lock the attributes.
     */
    freeze<T extends {[idx: string]: U | null | undefined | object}, U extends string | bigint | number | boolean | symbol>(o: T): Readonly<T>;

I think the lib.es5.d.ts should not contain type definition that uses bigint

Please let me know if this should go to https://github.com/microsoft/TypeScript-DOM-lib-generator/issues instead.

@fatcerberus
Copy link

I think the lib.es5.d.ts should not contain type definition that uses bigint

That's a necessary evil afaik because all the later targets reference lib.es5.d.ts and the function definition can't be modified once it's been declared.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 13, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.7.1 milestone May 13, 2022
@DanielRosenwasser
Copy link
Member

@RyanCavanaugh 4.7.1, 4.7.2, or 4.8.0?

@RyanCavanaugh
Copy link
Member

4.7.2

@sandersn
Copy link
Member

I think there's an error when the target is less than es2020 too, but errors resolving BigInt are suppressed when target is less than es2020.

This happens in getContextualSignature, which only runs on object literal methods. So it's an error for { f() { } } and { f: function () { } } but not for { f: () => { } }.

@sandersn
Copy link
Member

Broken by #33767

@sandersn
Copy link
Member

I'm not sure what the right long-term fix is, but the short-term fix is to remove bigint from es5.d.ts.

@sandersn
Copy link
Member

The current behaviour for BigInt is to fall back to {} when interface BigInt is not declared globally. Additionally, issue an error when target >= es2020.

Some better fixes:

  1. Issue a new error when the lib is older than the target. (newer? different?).
  2. Issue this error when target >= es2020 AND target === lib.
  3. Issue this error when target >= es2020 AND lib >= es2020.
  4. Issue this error when lib >= es2020.

(1) is probably separate from (2-4). I don't know what should be allowed here -- for example, as long as the emit for es2020 doesn't rely on es2015 stdlib declarations, it should be fine to have target=es2020 and lib=es5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants