Skip to content

bun build --dts: declare a constant that is not finite as number in .aot.ts, and an alias of true | false as boolean - #48485

Open
robobun wants to merge 1 commit into
mainfrom
robobun/895af0f8/dts-constant-text
Open

robobun wants to merge 1 commit into
mainfrom
robobun/895af0f8/dts-constant-text

Conversation

@robobun

@robobun robobun commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • bun build --dts with const big = 1e999 that only a type names: warn: the .aot.ts files that --aot compiles a package from are left out: they have type errors, the first at index.aot.ts:3:20: TS2749: 'Infinity' refers to a value, but is being used as a type here. Did you mean 'typeof Infinity'?
  • An alias of true | false is declared by its name, which can be another type in the bundle: TS2322: Type 'boolean' is not assignable to type 'Flag'.
  • Cause: module_types (src/sema/driver/typed_files.rs:4396) takes the text of the declaration from the type printer.

Fix

  • Such a constant is declared number, and boolean.
  • Other literals and keywords keep the text of the printer.
  • Verified: test/bundler/bun-build-api-dts.test.ts, test/cli/compile/compile-library.test.ts. 2 new tests fail on main.
  • Self-reviewed as a part of a larger diff: 4 concerns on this part, 4 addressed.

Background

  • index.aot.ts is index.js with the source types put back. One type error leaves out all of them.
  • A constant that only a type names (typeof big) is not in the bundle. The typed file declares it: declare const big: T;.
  • Considered: the type 1e999. The bundle has Infinity for the value, which does not fit it.

Downsides

  • typeof big is number in the typed file, and the type of 1e999 in the sources. index.d.ts says number.
  • Not fixed: a tuple or an object with such a value (one test.todo row).
  • Other builds: one lookup per constant that only a type names, the same 6,251 allocator calls. Binary text: 256 bytes more.
Notes

Repros (each is a package with "strict": true, built with bun build ./src/index.ts --dts --outdir out. Main exits 0, prints the warning and writes index.js and index.d.ts only.)

// src/index.ts
const big = 1e999;
export type Big = typeof big;
export function biggest(): Big {
  return 1e999;
}

Main writes declare const big: Infinity; into the typed file (TS2749). -1e999 gives declare const small: -Infinity; and TS1110: Type expected. This branch writes declare const big: number;.

// src/flag.ts
type Flag = true | false;
declare const stored: Flag;
export const on = stored;

// src/index.ts
import { on } from "./flag";
type Flag = "yes" | "no";
export type On = typeof on;
export function pick(flag: Flag): On {
  return flag === "yes";
}

Main writes declare const on: Flag;, and Flag is the type of src/index.ts there: index.aot.ts:7:3: TS2322: Type 'boolean' is not assignable to type 'Flag'. This branch writes declare const on: boolean;.

The change

text_that_names_nothing has the test that main has (the type is a literal or a keyword, and no member of an enum) and takes the text from the printer, but for two types:

  • A true | false union. It is one type with and without an alias, and the printer names the alias that the source has.
  • A number literal that is not finite. The printer says Infinity or -Infinity, which are values.

1e999 is a literal type that TypeScript accepts (declare const big: 1e999). It is not the text here because the bundle has Infinity for the value, so return Infinity in biggest() does not type check against it. index.d.ts of the same build has declare const big = Infinity;, which is number for whoever reads it.

Measured (main 98cda0d and this branch)

  • size of the release binary: text 142,830,298 bytes on main, 142,830,554 on this branch. The file is 143,018,256 and 143,022,352 bytes.
  • Allocator calls in module_types and in add_types, counted under gdb on debug builds, for a module with 300 functions and 100 constants that only a type names: 6,251 and 23,545 on both.
  • 17 constants of other types (strings, numbers, bigints, true, false, null, undefined, a template, a unique symbol) in one module: index.aot.ts is the same bytes on both.

Not fixed

  • const pair = [1e999, 1] as const that only a type names: declare const pair: readonly [Infinity, 1]; (TS2749), as on main. The text for a constant whose type is no literal and no keyword is the one that index.d.ts has.
  • A build that leaves the typed files out does not remove an index.aot.ts that an earlier build wrote into the same outdir. That is main too.

Suites run (debug ASAN build of this branch)

test/bundler/bun-build-api-dts.test.ts: 8 pass, 1 todo, and "writes the bundle, its declaration files and the bundle with its types kept to outdir" runs into the 5 s limit in a debug build here (5.0 s, and 5.2 s on a debug build of main). test/cli/compile/compile-library.test.ts with a longer time limit: the new test passes. On a debug build of main the two new tests fail.

Origin

This was a part of #48267. A review of #48267 asked for the parts that need no React Compiler as a PR of their own. #48466 has the enum part of those, so this PR has the constants alone.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/cli/compile/compile-library.test.ts

…nd an alias of `true | false` as `boolean`

A constant that only a type names is declared in the typed file with the
text that the type printer has for its type. The printer says `Infinity`
for the type of `1e999`, which is a value and no type, and it names the
alias of a `true | false` union, which can be another type where the
constant is declared.
@robobun

robobun commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

Status: ready for review at 73a0a8a.

Reproduced on release and debug builds of main 98cda0d. A package whose src/index.ts has const big = 1e999; and export type Big = typeof big; is built with bun build ./src/index.ts --dts --outdir out. Main exits 0, writes no index.aot.ts and warns TS2749: 'Infinity' refers to a value, but is being used as a type here. The second repro has a constant whose type is an alias type Flag = true | false of another file: TS2322: Type 'boolean' is not assignable to type 'Flag'.

With this branch the typed file has declare const big: number; and declare const on: boolean;.

PR: #48485

@robobun

robobun commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 11:31 AM PT - Nov 10th, 2026

✅ @robobun, your commit 73a0a8a51a6c3b41b0358f310a2e637606a59d35 passed in Build #139799! 🎉


🧪   To try this PR locally:

bunx bun-pr 48485

That installs a local version of the PR into your bun-48485 executable, so you can run:

bun-48485 --bun

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The constant type collection path now formats qualifying literal and intrinsic types with a new helper. Added tests check generated declaration output for boolean values, non-finite numeric literals, retained literal types, and library builds.

Changes

Declaration constant types

Layer / File(s) Summary
Format collected constant types
src/sema/driver/typed_files.rs
A new helper formats qualifying types, including boolean types as boolean and non-finite numeric literals as number. module_types uses the helper and retains its unique symbol handling.
Check declaration output
test/bundler/bun-build-api-dts.test.ts, test/cli/compile/compile-library.test.ts
Tests check generated declaration types for boolean values, non-finite numeric literals, and other literal values. A compile-library test checks the generated types for big and word. A todo test records a tuple issue without asserting an outcome.

Possibly related PRs

  • oven-sh/bun#48267: Uses the shared constant-type text helper for moved default parameter types.

Suggested reviewers: jarred-sumner

Merge Risk: Minimal · up to 73a0a

The declaration changes have no established issue requiring a fix before merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title clearly summarizes both main changes: declaring non-finite numeric constants as number and true | false aliases as boolean. It is longer than necessary, but it is specific and relevant…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the template's two sections, "What does this PR do?" (as "Problem", "Fix", "Background", "Downsides") and "How did you verify your code works?" (as "Suites run" and "Measured" …
Full details: Title check

Explanation

The title clearly summarizes both main changes: declaring non-finite numeric constants as number and true | false aliases as boolean. It is longer than necessary, but it is specific and relevant.

Full details: Description check

Explanation

The description follows the template's two sections, "What does this PR do?" (as "Problem", "Fix", "Background", "Downsides") and "How did you verify your code works?" (as "Suites run" and "Measured" in the notes). It gives concrete repros, the root cause (module_types takes the type printer's text at typed_files.rs:4396), the exact fix for two cases, measured effects on binary size and allocator calls, and known gaps (tuple case left as a test.todo). The verification section names both test files and says two new tests fail on main. The remaining gaps are minor: the bundler suite has one test that hits the 5 s limit in a debug build, and the CI-only note says compile-library.test.ts is platform-specific and deferred to CI. Those points are reported openly, so the description is mostly complete.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 89452d2 and 73a0a8a.

Files selected for processing: 3

  • src/sema/driver/typed_files.rs
  • test/bundler/bun-build-api-dts.test.ts
  • test/cli/compile/compile-library.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I also checked two edges of text_that_names_nothing: the boolean branch cannot widen a bare true/false constant, because TypeFlags::BOOLEAN is only set on the two-member true | false union (src/sema/types.rs flags computation) while a lone literal carries BOOLEAN_LITERAL; and the non-finite branch cannot catch NaN, since no source literal produces a NaN literal type (NaN itself types as number). I did not build or run the two new tests.

Extended reasoning...

The change extracts the declare const text selection in src/sema/driver/typed_files.rs into a helper and adds two overrides (boolean for an aliased true | false, number for a non-finite number literal), with tests in test/bundler/bun-build-api-dts.test.ts and test/cli/compile/compile-library.test.ts. It touches no security-sensitive surface. The one inline finding is the as const tuple/object sibling case the PR leaves as a test.todo, so a human should decide whether that scope is acceptable.

Comment thread src/sema/driver/typed_files.rs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant