These are the type definitions for Bun's JavaScript runtime APIs.
Install the bun-types
npm package:
# yarn/npm/pnpm work too, "bun-types" is an ordinary npm package
bun add bun-types
Add this to your tsconfig.json
or jsconfig.json
:
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
// "bun-types" is the important part
"types": ["bun-types"]
}
}
bun-types
is generated via ./bundle.ts.
- Add it to ./index.d.ts
./bundle.ts
merges the types in this folder into a single file.
To run it:
bun generate
bun-types / Exports
- "bun"
- "bun:ffi"
- "bun:jsc"
- "bun:sqlite"
- "bun:test"
- "node:path/posix"
- "node:path/win32"
- "node:sys"
- "path/posix"
- "path/win32"
- "supports-color"
- "sys"
- "test"
- HTMLRewriterTypes
- AbortSignalEventMap
- AddEventListenerOptions
- BlobInterface
- BlobPropertyBag
- BufferConstructor
- BufferEncodingOption
- ByteLengthQueuingStrategy
- CloseEvent
- CloseEventInit
- CountQueuingStrategy
- Crypto
- Dict
- DirectUnderlyingSource
- EncodeIntoResult
- ErrnoException
- ErrorEvent
- ErrorEventInit
- Event
- EventInit
- EventListener
- EventListenerObject
- EventListenerOptions
- EventMap
- EventModifierInit
- EventSourceInit
- EventTarget
- FetchEvent
- GenericTransformStream
- Headers
- MessageEvent
- MessageEventInit
- Process
- QueuingStrategy
- QueuingStrategyInit
- QueuingStrategySize
- ReadOnlyDict
- ReadWriteStream
- ReadableStream
- ReadableStreamDefaultController
- ReadableStreamDefaultReadDoneResult
- ReadableStreamDefaultReadValueResult
- ReadableStreamDefaultReader
- ReadableStreamDirectController
- ReadableStreamGenericReader
- ReadableWritablePair
- RequestInit
- ResponseInit
- StreamPipeOptions
- StructuredSerializeOptions
- TransformStream
- TransformStreamDefaultController
- Transformer
- TransformerFlushCallback
- TransformerStartCallback
- TransformerTransformCallback
- UIEventInit
- UnderlyingSink
- UnderlyingSinkAbortCallback
- UnderlyingSinkCloseCallback
- UnderlyingSinkStartCallback
- UnderlyingSinkWriteCallback
- UnderlyingSource
- UnderlyingSourceCancelCallback
- UnderlyingSourcePullCallback
- UnderlyingSourceStartCallback
- VoidFunction
- WebSocket
- WebSocketEventMap
- WritableStream
- WritableStreamDefaultController
- WritableStreamDefaultWriter
- Bun
- ByteLengthQueuingStrategy
- CloseEvent
- CountQueuingStrategy
- ErrorEvent
- Event
- EventTarget
- Headers
- Loader
- MessageEvent
- ReadableStream
- ReadableStreamDefaultController
- ReadableStreamDefaultReader
- TransformStream
- TransformStreamDefaultController
- WebSocket
- WritableStream
- WritableStreamDefaultController
- WritableStreamDefaultWriter
- crypto
- performance
• Bun: "bun"
dist/types.d.ts:14263
• ByteLengthQueuingStrategy: Object
Name | Type |
---|---|
prototype |
ByteLengthQueuingStrategy |
dist/types.d.ts:2126
dist/types.d.ts:2131
• CloseEvent: Object
Name | Type |
---|---|
prototype |
CloseEvent |
dist/types.d.ts:1712
dist/types.d.ts:1721
• CountQueuingStrategy: Object
Name | Type |
---|---|
prototype |
CountQueuingStrategy |
dist/types.d.ts:2362
dist/types.d.ts:2367
• ErrorEvent: Object
Name | Type |
---|---|
prototype |
ErrorEvent |
dist/types.d.ts:1698
dist/types.d.ts:1706
• Event: Object
Name | Type |
---|---|
AT_TARGET |
number |
BUBBLING_PHASE |
number |
CAPTURING_PHASE |
number |
NONE |
number |
prototype |
Event |
dist/types.d.ts:1588
dist/types.d.ts:1686
• EventTarget: Object
Name | Type |
---|---|
prototype |
EventTarget |
dist/types.d.ts:1536
dist/types.d.ts:1582
• Headers: Object
Name | Type |
---|---|
prototype |
Headers |
dist/types.d.ts:682
dist/types.d.ts:697
• Loader: Object
Low-level JavaScriptCore API for accessing the native ES Module loader (not a Bun API)
Before using this, be aware of a few things:
Using this incorrectly will crash your application.
This API may change any time JavaScriptCore is updated.
Bun may rewrite ESM import specifiers to point to bundled code. This will be confusing when using this API, as it will return a string like "/node_modules.server.bun".
Bun may inject additional imports into your code. This usually has a bun:
prefix.
Name | Type | Description |
---|---|---|
dependencyKeysIfEvaluated |
(specifier : string ) => string [] |
For an already-evaluated module, return the dependencies as module specifiers This list is already sorted and uniqued. Example For this code: js // /foo.js import classNames from 'classnames'; import React from 'react'; import {createElement} from 'react'; This would return: js Loader.dependencyKeysIfEvaluated("/foo.js") ["bun:wrap", "/path/to/node_modules/classnames/index.js", "/path/to/node_modules/react/index.js"] |
registry |
Map <string , { dependencies : string [] ; module : any ; state : number }> |
ESM module registry This lets you implement live reload in Bun. If you delete a module specifier from this map, the next time it's imported, it will be re-transpiled and loaded again. The keys are the module specifiers and the values are metadata about the module. The keys are an implementation detail for Bun that will change between versions. - Userland modules are an absolute file path - Virtual modules have a bun: prefix or node: prefix - JS polyfills start with "/bun-vfs/" . "buffer" is an example of a JS polyfill - If you have a node_modules.bun file, many modules will point to that file Virtual modules and JS polyfills are embedded in bun's binary. They don't point to anywhere in your local filesystem. |
resolve |
(specifier : string ) => Promise <string > |
The function JavaScriptCore internally calls when you use an import statement. This may return a path to node_modules.server.bun , which will be confusing. Consider Bun.resolve or ImportMeta.resolve instead. |
resolveSync |
(specifier : string , from : string ) => string |
Synchronously resolve a module specifier This may return a path to node_modules.server.bun , which will be confusing. Consider Bun.resolveSync instead. |
dist/types.d.ts:1994
• MessageEvent: Object
Name | Type |
---|---|
prototype |
MessageEvent <any > |
dist/types.d.ts:1727
dist/types.d.ts:1747
• ReadableStream: Object
Name | Type |
---|---|
prototype |
ReadableStream <any > |
dist/types.d.ts:2084
dist/types.d.ts:2103
• ReadableStreamDefaultController: Object
Name | Type |
---|---|
prototype |
ReadableStreamDefaultController <any > |
dist/types.d.ts:2136
dist/types.d.ts:2151
• ReadableStreamDefaultReader: Object
Name | Type |
---|---|
prototype |
ReadableStreamDefaultReader <any > |
dist/types.d.ts:2156
dist/types.d.ts:2162
• TransformStream: Object
Name | Type |
---|---|
prototype |
TransformStream <any , any > |
dist/types.d.ts:2311
dist/types.d.ts:2316
• TransformStreamDefaultController: Object
Name | Type |
---|---|
prototype |
TransformStreamDefaultController <any > |
dist/types.d.ts:2325
dist/types.d.ts:2332
• WebSocket: Object
Name | Type |
---|---|
CLOSED |
number |
CLOSING |
number |
CONNECTING |
number |
OPEN |
number |
prototype |
WebSocket |
dist/types.d.ts:1763
dist/types.d.ts:1818
• WritableStream: Object
Name | Type |
---|---|
prototype |
WritableStream <any > |
dist/types.d.ts:2193
dist/types.d.ts:2200
• WritableStreamDefaultController: Object
Name | Type |
---|---|
prototype |
WritableStreamDefaultController |
dist/types.d.ts:2209
dist/types.d.ts:2213
• WritableStreamDefaultWriter: Object
Name | Type |
---|---|
prototype |
WritableStreamDefaultWriter <any > |
dist/types.d.ts:2219
dist/types.d.ts:2229
• crypto: Crypto
dist/types.d.ts:1165
• performance: Object
Name | Type |
---|---|
now |
() => number |
dist/types.d.ts:1363
▸ addEventListener<K
>(type
, listener
, options?
): void
Name | Type |
---|---|
K |
extends "fetch" |
Name | Type |
---|---|
type |
K |
listener |
(this : object , ev : EventMap [K ]) => any |
options? |
boolean | AddEventListenerOptions |
void
▸ addEventListener(type
, listener
, options?
): void
Name | Type |
---|---|
type |
string |
listener |
EventListenerOrEventListenerObject |
options? |
boolean | AddEventListenerOptions |
void
▸ addEventListener<K
>(type
, listener
, options?
): void
Name | Type |
---|---|
K |
extends "fetch" |
Name | Type |
---|---|
type |
K |
listener |
(this : object , ev : EventMap [K ]) => any |
options? |
boolean | AddEventListenerOptions |
void
dist/types.d.ts:1439
▸ addEventListener(type
, listener
, options?
): void
Name | Type |
---|---|
type |
string |
listener |
EventListenerOrEventListenerObject |
options? |
boolean | AddEventListenerOptions |
void
dist/types.d.ts:1444
▸ alert(message?
): void
Name | Type |
---|---|
message? |
string |
void
▸ alert(message?
): void
Name | Type |
---|---|
message? |
string |
void
dist/types.d.ts:2399
▸ atob(encodedData
): string
atob
decodes base64 into ascii text.
Name | Type |
---|---|
encodedData |
string |
string
▸ atob(encodedData
): string
atob
decodes base64 into ascii text.
Name | Type |
---|---|
encodedData |
string |
string
dist/types.d.ts:1172
▸ btoa(stringToEncode
): string
btoa
encodes ascii text into base64.
Name | Type | Description |
---|---|---|
stringToEncode |
string |
The ascii text to encode. |
string
▸ btoa(stringToEncode
): string
btoa
encodes ascii text into base64.
Name | Type | Description |
---|---|---|
stringToEncode |
string |
The ascii text to encode. |
string
dist/types.d.ts:1179
▸ confirm(message?
): boolean
Name | Type |
---|---|
message? |
string |
boolean
▸ confirm(message?
): boolean
Name | Type |
---|---|
message? |
string |
boolean
dist/types.d.ts:2400
▸ fetch(url
, init?
): Promise
<Response
>
Send a HTTP(s) request
Name | Type | Description |
---|---|---|
url |
string |
URL string |
init? |
RequestInit |
A structured value that contains settings for the fetch() request. |
Promise
<Response
>
A promise that resolves to Response object.
▸ fetch(request
, init?
): Promise
<Response
>
Send a HTTP(s) request
Name | Type | Description |
---|---|---|
request |
Request |
Request object |
init? |
RequestInit |
A structured value that contains settings for the fetch() request. |
Promise
<Response
>
A promise that resolves to Response object.
▸ fetch(url
, init?
): Promise
<Response
>
Send a HTTP(s) request
Name | Type | Description |
---|---|---|
url |
string |
URL string |
init? |
RequestInit |
A structured value that contains settings for the fetch() request. |
Promise
<Response
>
A promise that resolves to Response object.
dist/types.d.ts:1398
▸ fetch(request
, init?
): Promise
<Response
>
Send a HTTP(s) request
Name | Type | Description |
---|---|---|
request |
Request |
Request object |
init? |
RequestInit |
A structured value that contains settings for the fetch() request. |
Promise
<Response
>
A promise that resolves to Response object.
dist/types.d.ts:1411
▸ prompt(message?
, _default?
): string
| null
Name | Type |
---|---|
message? |
string |
_default? |
string |
string
| null
▸ prompt(message?
, _default?
): string
| null
Name | Type |
---|---|
message? |
string |
_default? |
string |
string
| null
dist/types.d.ts:2401
▸ removeEventListener<K
>(type
, listener
, options?
): void
Name | Type |
---|---|
K |
extends "fetch" |
Name | Type |
---|---|
type |
K |
listener |
(this : object , ev : EventMap [K ]) => any |
options? |
boolean | EventListenerOptions |
void
▸ removeEventListener(type
, listener
, options?
): void
Name | Type |
---|---|
type |
string |
listener |
EventListenerOrEventListenerObject |
options? |
boolean | EventListenerOptions |
void
▸ removeEventListener<K
>(type
, listener
, options?
): void
Name | Type |
---|---|
K |
extends "fetch" |
Name | Type |
---|---|
type |
K |
listener |
(this : object , ev : EventMap [K ]) => any |
options? |
boolean | EventListenerOptions |
void
dist/types.d.ts:1449
▸ removeEventListener(type
, listener
, options?
): void
Name | Type |
---|---|
type |
string |
listener |
EventListenerOrEventListenerObject |
options? |
boolean | EventListenerOptions |
void
dist/types.d.ts:1454
▸ reportError(error
): void
Log an error using the default exception handler
Name | Type | Description |
---|---|---|
error |
any |
Error or string |
void
▸ reportError(error
): void
Log an error using the default exception handler
Name | Type | Description |
---|---|---|
error |
any |
Error or string |
void
dist/types.d.ts:1418