oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.24k stars 2.77k forks source link

bun-types causes Typescript to ignore compilerOptions.lib #5134

Open CraigglesO opened 1 year ago

CraigglesO commented 1 year ago

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b

What platform is your computer?

Darwin 22.5.0 arm64 arm

What steps can reproduce the bug?

Work on a project that utilized typescript with a lib properly like so:

{
  "compilerOptions": {
    "target": "esnext",
    "lib": [
      "DOM",
      "DOM.Iterable",
      "ESNext",
      "WebWorker"
    ],
...
}

Install bun-types as a dependency or dev-dependency:

bun i -D bun-types

What is the expected behavior?

My library depends upon the DOM and DOM.Iterable at a minimum. No errors occur and tsc will properly build types / the codebase

What do you see instead?

Screenshot 2023-09-12 at 1 44 58 PM

Webpack refuses to build because DOM library doesn't exist.

As soon as I remove bun-types, remove node-modules, and reinstall bun i everything works agian.

This is a private repository, so if you want me to go out searching for a repo you can perfectly duplicate I can. Figured you might have an idea as to what is causing this without me doing a bunch of extra work.

Additional information

No response

bjon commented 1 year ago

We have the same problem. Only thing that stops us from using bun test.

colinhacks commented 1 year ago

This is an unfortunately consequence of adding "types": ["bun-types"] in tsconfig.

Per https://bun.sh/docs/test/dom, the fix is to add this line at the top of a file somewhere in your project:

/// <reference lib="dom" />

You can replace "dom" with any lib you want to be added into your project. We're working on better solutions but currently this is the best way.

CraigglesO commented 1 year ago

Yep that works. Added a .d.ts file like so:

/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="esnext" />
/// <reference lib="webworker" />

Happy enough with this solution for now since bun has such a massive speedup. Appreciate it. Will leave this open as a bug so that I can properly track.

johnschoeman commented 1 year ago

Ran into this issue as well and found an alternate solution of removing this line:

/// <reference no-default-lib="true" />

from node_modules/bun-types/types.d.ts

Which I think works okay for a solo project and it's nice to not have to make changes to your own application code to introduce bun.


The directive can be found in source here: https://github.com/oven-sh/bun/blame/main/packages/bun-types/header.txt#L5

and was added in this commit: https://github.com/oven-sh/bun/commit/f7f1b604443c030afe29d1059b90f72c69afe081

But it's not clear to me why it's needed or if removing it has other consequences.