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

Generate type declarations during bun build #5141

Open
kynnyhsap opened this issue Sep 12, 2023 · 11 comments
Open

Generate type declarations during bun build #5141

kynnyhsap opened this issue Sep 12, 2023 · 11 comments
Labels
enhancement New feature or request typescript Something for TypeScript

Comments

@kynnyhsap
Copy link

What is the problem this feature would solve?

I'd like to build my library written in typescript with bun build (both for dev and prod), but there's no point in doing so if I or my library's users won't have type declarations. I am developing this library in the ecosystem of other projects, so often it looks like npm run dev:my-library && npm run dev:my-frontend. This is a common pattern for developing in monorepos. My current solution is tsc, which is slow in watch mode and has its drawbacks.

What is the feature you are proposing to solve the problem?

--type-declarations flag for build command to generate types along with js files.

What alternatives have you considered?

I see no workaround.

@kynnyhsap kynnyhsap added the enhancement New feature or request label Sep 12, 2023
@colinhacks
Copy link
Contributor

colinhacks commented Sep 12, 2023

This is out of scope for Bun. Currently tsc is the only tool in existence for reliably generating declaration files. Doing it right would require re-implementing the entire TypeScript compiler & type inference infrastructure unfortunately.

This may be possible in some limited cases eventually.

@The-Code-Monkey
Copy link

The-Code-Monkey commented Sep 14, 2023

@colinhacks I would have to disagree with you, the whole point of bun which is pointed out it to be a compiler, typescript, test runner and more. If you haven't implemented one of the main features of typescript which is types, how can you expect people to build a typescript library with it, as there will never be any type definitions.

And also Im pretty sure on https://bun.sh/blog/bun-v1.0 it litterally states that it is a replacement for tsc apart from typechecking, typechecking is fine but the actual usage of compiled types for development is a massive dealbreaker for me and probably will be for others.

Due to the reasons most people would want to move away from tsc is because speed but your just telling us to use it anyway.

Edit: because of this inability to generate types using bun, bun therefore isnt a drop in replacement for node and tsc, and actually breaks the functionality of libs.

@tomquirk
Copy link

tomquirk commented Sep 14, 2023

It seems like given this stance, library authors who want to provide types can't really use bun build for their libs/packages (every author except DHH) 🤷 .

Not a dealbreaker (can still use other parts of bun), but worth noting.

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Sep 14, 2023

My expectation is that Isolated Declarations will eventually ship in TypeScript and then some number of weeks or months later, we will ship support for emitting TypeScript types in bun build(for projects leveraging isolated declarations)

@The-Code-Monkey
Copy link

👍 Thanks for clarification @Jarred-Sumner will this be something that bun will automatically do, if it notices the repo is using typescript

@paperdave
Copy link
Collaborator

When isolated declarations is stabilized, I think Bun should support that. It'll let us generate the d.ts files quickly, in exchange for some requirements on the source code (have to annotate each exported fn with a return type)

For now, a third-party tool i use on my own projects is https://github.com/timocov/dts-bundle-generator.
Someone from the community even made a plugin for it: https://github.com/wobsoriano/bun-plugin-dts

To me, that's more of a workaround than a real solution (it's quite slow), but it works for now.

@joaopaulomoraes
Copy link

As a workaround, you can generate the type declaration with just tsc in an additional command in your package.json.

Eg:

"scripts": {
  "build": "bun build ... && bun run build:declaration",
  "build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json"
}

So you have the build followed by the generation of declarations that can be added to the build command or executed separately.

@danielfalk
Copy link

As a workaround, you can generate the type declaration with just tsc in an additional command in your package.json.

Eg:

"scripts": {
  "build": "bun build ... && bun run build:declaration",
  "build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json"
}

So you have the build followed by the generation of declarations that can be added to the build command or executed separately.

Is there any advantage to doing this as opposed to just using tsc for building? It seems like it would be slower to have two different process for building that have to run in sequence.

@JacobWeisenburger
Copy link

https://github.com/wobsoriano/bun-plugin-dts

@ryoppippi
Copy link

https://github.com/ryoppippi/bun-plugin-isolated-decl

@RobinTail
Copy link

RobinTail commented Jul 5, 2024

Could bun operate tsc internally and build a declaration file of the bundle like tsup does?

https://tsup.egoist.dev/#generate-declaration-file

tsupbun
export default defineConfig({
  entry: ["src/index.ts"],
  format: ["esm", "cjs"],
  splitting: false,
  sourcemap: false,
  minify: true,
  clean: true,
  dts: true,
  platform: "browser",
});
await Bun.build({
  entrypoints: ["src/index.ts"],
  format: "esm", // CJS — missing
  splitting: false,
  sourcemap: "none",
  minify: true,
  outdir: "dist",
  // dts — missing
  target: "browser",
});

@JacobWeisenburger
https://github.com/wobsoriano/bun-plugin-dts

That plugin does not work at all:

[local] ➜  merge-sx git:(try-bun) ✗ bun build.ts
354 | }
355 | exports.getRootSourceFile = getRootSourceFile;
356 | function getNodeOwnSymbol(node, typeChecker) {
357 |     const nodeSymbol = typeChecker.getSymbolAtLocation(node);
358 |     if (nodeSymbol === undefined) {
359 |         throw new Error(`Cannot find symbol for node "${node.getText()}" in "${node.parent.getText()}" from "${node.getSourceFile().fileName}"`);
                    ^
error: Cannot find symbol for node "window" in "window.matchMedia"

@ryoppippi
https://github.com/ryoppippi/bun-plugin-isolated-decl

That one does work, with minor issue (fixed).
And it's also worth it to note that it wipes JSDoc out at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request typescript Something for TypeScript
Projects
None yet
Development

No branches or pull requests