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

types: Rewrite to avoid conflicts and allow for doc generation #18024

Open
wants to merge 83 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
459248b
changes
alii Mar 5, 2025
c0b8830
.
alii Mar 5, 2025
d6fc7bc
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 5, 2025
fab9ab9
attempt
alii Mar 5, 2025
f476d88
some changes
alii Mar 6, 2025
dde397c
YES!!
alii Mar 6, 2025
b59cbbb
fixes
alii Mar 7, 2025
596ebe2
changes
alii Mar 7, 2025
43c9547
some more changes
alii Mar 7, 2025
7d5de99
more stuff
alii Mar 7, 2025
d9353e1
Use Bun.__internal to hide extended imports that dont create ugly `_T…
alii Mar 7, 2025
34045dc
Only failing types are now in node!!!
alii Mar 7, 2025
2c25cd1
fix RequestInit types
alii Mar 7, 2025
fdf93dd
URL stuff
alii Mar 7, 2025
eeeec68
jsx, sqlite, html-rewriter, test
alii Mar 7, 2025
b419cc0
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 9, 2025
1e21cd6
changes
alii Mar 9, 2025
2b84554
rm formatting on eslint
alii Mar 9, 2025
0a83104
remove all final `type _Name = ...` declarations
alii Mar 9, 2025
34d12c8
Fix MessageEvent<T> and Bun namespace redeclaration
alii Mar 9, 2025
eeeab08
Remove type-only declaration for `'types'` specifier
alii Mar 9, 2025
733a571
oops
alii Mar 9, 2025
32f0080
make them pass
alii Mar 9, 2025
b4d53d0
update tsconfig references
alii Mar 9, 2025
6b1426c
work on bringing back support for DOM
alii Mar 9, 2025
acbe087
Don't conflict when `DOM` types are loaded. We can only prefer DOM ty…
alii Mar 9, 2025
6b38592
update typescript guide
alii Mar 9, 2025
02af6e7
add tests for DOM enabled and disabled
alii Mar 9, 2025
bb49f88
make it pass without dom
alii Mar 10, 2025
d14668f
fix test name
alii Mar 10, 2025
a4d183a
nothrow
alii Mar 10, 2025
c53a9a9
fix more types
alii Mar 10, 2025
4942bcb
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 10, 2025
7a9358c
some changes
alii Mar 10, 2025
04110ac
checkpoint
alii Mar 11, 2025
007a2bd
This works fine for overriding lib.dom.d.ts, and still works with node.
alii Mar 11, 2025
26405b3
Change how Bun's lib.dom.d.ts gets loaded
alii Mar 11, 2025
bdbf720
more type fixes
alii Mar 11, 2025
f1e50ee
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 11, 2025
f009831
undefined->void
alii Mar 11, 2025
bec5844
make them pass
alii Mar 11, 2025
99ea96a
bring back dom
alii Mar 11, 2025
17dfe5b
both tests pass!
alii Mar 12, 2025
be61cd2
test {body,headers}init exists
alii Mar 13, 2025
a23e807
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 13, 2025
127fcb3
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 13, 2025
6c9b822
sync new types to `main`
alii Mar 13, 2025
fc4fba4
new → root
alii Mar 13, 2025
a5d11b9
change svg decl in extensions.d.ts
alii Mar 13, 2025
70660c6
make devserver.d.ts non-module to fix namespace clash
alii Mar 13, 2025
4a19281
authoring
alii Mar 13, 2025
3c292ce
make clarifications in authoring.md
alii Mar 13, 2025
7e9cd47
update
alii Mar 13, 2025
1cba7d6
update authoring.md
alii Mar 13, 2025
78cf7fc
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 13, 2025
f475475
Merge branch 'main' into feat/doc-gen-bun-types
alii Mar 13, 2025
df459ea
fix: globally define RequestInit and ResponseInit
alii Mar 14, 2025
6d69cbe
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 14, 2025
e2f825d
fetch changes + some doc + test fixture work
alii Mar 14, 2025
fac04f0
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 14, 2025
7a98828
deps
alii Mar 14, 2025
a829ae8
isolate test fixture, remove BunGlobalSymbolRegistry for fetch declar…
alii Mar 17, 2025
9301f78
fix testing to check for Response.redirect failure when DOM is loaded
alii Mar 17, 2025
7d6cb8d
Merge branch 'main' of github.com:oven-sh/bun into feat/doc-gen-bun-t…
alii Mar 17, 2025
85c970c
use latest ts version
alii Mar 17, 2025
c159b6d
fix test to work cross-platform
alii Mar 17, 2025
347047b
test cases to check for issue described in #13167
alii Mar 17, 2025
db5adf4
higher tolerance for failing test error message
alii Mar 17, 2025
e387f81
update tsconfigs + test
alii Mar 17, 2025
e736c36
support URL.parse, Error.isError, add some more test cases
alii Mar 17, 2025
e9f1a93
do the cleanup
alii Mar 17, 2025
d57f61c
types: more test cases
alii Mar 17, 2025
3aa3ea6
docs supported tsconfig
alii Mar 17, 2025
5db7eb6
remove talk of bundle.ts (doesnt exist)
alii Mar 17, 2025
b64cd17
add jsdocs
alii Mar 17, 2025
bdaf9d1
Support more documentation
alii Mar 17, 2025
1b240be
remove tsconfig
alii Mar 17, 2025
8b070ca
Blob#text
alii Mar 17, 2025
b3fd8c7
fix HMREventNames
alii Mar 17, 2025
eae8736
Others
alii Mar 17, 2025
adb9e05
test direct readablestream
alii Mar 17, 2025
11d4a88
move fetch declarations to globals.d.ts for doc gen purposes
alii Mar 17, 2025
5b36ee6
jsdoc for fetch()
alii Mar 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/express/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
Expand Down
2 changes: 1 addition & 1 deletion bench/postgres/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
Expand Down
3 changes: 2 additions & 1 deletion bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
},
"packages/bun-types": {
"name": "bun-types",
"version": "1.2.5",
"dependencies": {
"@types/node": "*",
"@types/ws": "~8.5.10",
"@types/ws": "*",
},
"devDependencies": {
"@biomejs/biome": "^1.5.3",
Expand Down
15 changes: 8 additions & 7 deletions docs/guides/runtime/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Below is the full set of recommended `compilerOptions` for a Bun project. With t
```jsonc
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
// Environment setup & latest features
"lib": ["esnext", "dom"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
Expand All @@ -31,13 +31,14 @@ Below is the full set of recommended `compilerOptions` for a Bun project. With t

// Best practices
"strict": true,
"skipLibCheck": true,
"skipLibCheck": false,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,

// Some stricter flags
"noUnusedLocals": true,
"noUnusedParameters": true,
"noPropertyAccessFromIndexSignature": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
},
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Bun supports things like top-level await, JSX, and extensioned `.ts` imports, wh
// Some stricter flags
"noUnusedLocals": true,
"noUnusedParameters": true,
"noPropertyAccessFromIndexSignature": true
}
"noPropertyAccessFromIndexSignature": true,
},
}
```

Expand Down
4 changes: 1 addition & 3 deletions packages/bun-types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ That's it! VS Code and TypeScript automatically load `@types/*` packages into yo

# Contributing

The `@types/bun` package is a shim that loads `bun-types`. The `bun-types` package lives in the Bun repo under `packages/bun-types`. It is generated via [./scripts/bundle.ts](./scripts/bundle.ts).
The `@types/bun` package is a shim that loads `bun-types`. The `bun-types` package lives in the Bun repo under `packages/bun-types`.

To add a new file, add it under `packages/bun-types`. Then add a [triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) pointing to it inside [./index.d.ts](./index.d.ts).

```diff
+ /// <reference path="./newfile.d.ts" />
```

[`./bundle.ts`](./bundle.ts) merges the types in this folder into a single file. To run it:

```bash
bun build
```
114 changes: 114 additions & 0 deletions packages/bun-types/authoring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Authoring @types/bun

These declarations define the `'bun'` module, the `Bun` global variable, and lots of other global declarations like extending the `fetch` interface.

## The `'bun'` Module

The `Bun` global variable and the `'bun'` module types are defined with one syntax. It supports declaring both types/interfaces and runtime values:

```typescript
declare module "bun" {
// Your types go here
interface MyInterface {
// ...
}

type MyType = string | number;

function myFunction(): void;
}
```

You can write as many `declare module "bun"` declarations as you need. Symbols will be accessible from other files inside of the declaration, and they will all be merged when the types are evaluated.

You can consume these declarations in two ways:

1. Importing it from `'bun'`:

```typescript
import { type MyInterface, type MyType, myFunction } from "bun";

const myInterface: MyInterface = {};
const myType: MyType = "cool";
myFunction();
```

2. Using the global `Bun` object:

```typescript
const myInterface: Bun.MyInterface = {};
const myType: Bun.MyType = "cool";
Bun.myFunction();
```

Consuming them inside the ambient declarations is also easy:

```ts
// These are equivalent
type A = import("bun").MyType;
type A = Bun.MyType;
```

## File Structure

Types are organized across multiple `.d.ts` files in the `packages/bun-types` directory:

- `index.d.ts` - The main entry point that references all other type files
- `bun.d.ts` - Core Bun APIs and types
- `globals.d.ts` - Global type declarations
- `test.d.ts` - Testing-related types
- `sqlite.d.ts` - SQLite-related types
- ...etc. You can make more files

Note: The order of references in `index.d.ts` is important - `bun.ns.d.ts` must be referenced last to ensure the `Bun` global gets defined properly.

### Best Practices

1. **Type Safety**

- Please use strict types instead of `any` where possible
- Leverage TypeScript's type system features (generics, unions, etc.)
- Document complex types with JSDoc comments

2. **Compatibility**

- Use `Bun.__internal.UseLibDomIfAvailable<LibDomName extends string, OurType>` for types that might conflict with lib.dom.d.ts (see [`./fetch.d.ts`](./fetch.d.ts) for a real example)
- `@types/node` often expects variables to always be defined (this was the biggest cause of most of the conflicts in the past!), so we use the `UseLibDomIfAvailable` type to make sure we don't overwrite `lib.dom.d.ts` but still provide Bun types while simultaneously declaring the variable exists (for Node to work) in the cases that we can.

3. **Documentation**
- Add JSDoc comments for public APIs
- Include examples in documentation when helpful
- Document default values and important behaviors

### Internal Types

For internal types that shouldn't be exposed to users, use the `__internal` namespace:

```typescript
declare module "bun" {
namespace __internal {
interface MyInternalType {
// ...
}
}
}
```

The internal namespace is mostly used for declaring things that shouldn't be globally accessible on the `bun` namespace, but are still used in public apis. You can see a pretty good example of that in the [`./fetch.d.ts`](./fetch.d.ts) file.

## Testing Types

We test our type definitions using a special test file at `fixture/index.ts`. This file contains TypeScript code that exercises our type definitions, but is never actually executed - it's only used to verify that the types work correctly.

The test file is type-checked in two different environments:

1. With `lib.dom.d.ts` included - This simulates usage in a browser environment where DOM types are available
2. Without `lib.dom.d.ts` - This simulates usage in a server-like environment without DOM types

Your type definitions must work properly in both environments. This ensures that Bun's types are compatible regardless of whether DOM types are present or not.

For example, if you're adding types for a new API, you should just add code to `fixture/index.ts` that uses your new API. Doesn't need to work at runtime (e.g. you can fake api keys for example), it's just checking that the types are correct.

## Questions

Feel free to open an issue or speak to any of the more TypeScript-focused team members if you need help authoring types or fixing type tests.
Loading