Skip to content

Instantly share code, notes, and snippets.

@valgaze
Last active July 27, 2022 09:42
Show Gist options
  • Save valgaze/04dde2c997c016e0640256afb616b23d to your computer and use it in GitHub Desktop.
Save valgaze/04dde2c997c016e0640256afb616b23d to your computer and use it in GitHub Desktop.
Root readme

Bun TypeScript type definitions

Logo

These are the type definitions for Bun's JavaScript runtime APIs.

Installation

Install the bun-types npm package:

# yarn/npm/pnpm work too, "bun-types" is an ordinary npm package
bun add bun-types

Usage

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"]
  }
}

Contributing

bun-types is generated via ./bundle.ts.

Adding a new file

  1. Add it to ./index.d.ts

How to generate types.d.ts

./bundle.ts merges the types in this folder into a single file.

To run it:

bun generate

Generated docs

bun-types / Exports

bun-types

Table of contents

Namespaces

Interfaces

Variables

Functions

Variables

Bun

Bun: "bun"

Defined in

bun.d.ts:1233

dist/types.d.ts:14263


ByteLengthQueuingStrategy

ByteLengthQueuingStrategy: Object

Type declaration

Name Type
prototype ByteLengthQueuingStrategy

Defined in

globals.d.ts:1715

globals.d.ts:1720

dist/types.d.ts:2126

dist/types.d.ts:2131


CloseEvent

CloseEvent: Object

Type declaration

Name Type
prototype CloseEvent

Defined in

globals.d.ts:1301

globals.d.ts:1310

dist/types.d.ts:1712

dist/types.d.ts:1721


CountQueuingStrategy

CountQueuingStrategy: Object

Type declaration

Name Type
prototype CountQueuingStrategy

Defined in

globals.d.ts:1951

globals.d.ts:1956

dist/types.d.ts:2362

dist/types.d.ts:2367


ErrorEvent

ErrorEvent: Object

Type declaration

Name Type
prototype ErrorEvent

Defined in

globals.d.ts:1287

globals.d.ts:1295

dist/types.d.ts:1698

dist/types.d.ts:1706


Event

Event: Object

Type declaration

Name Type
AT_TARGET number
BUBBLING_PHASE number
CAPTURING_PHASE number
NONE number
prototype Event

Defined in

globals.d.ts:1177

globals.d.ts:1275

dist/types.d.ts:1588

dist/types.d.ts:1686


EventTarget

EventTarget: Object

Type declaration

Name Type
prototype EventTarget

Defined in

globals.d.ts:1125

globals.d.ts:1171

dist/types.d.ts:1536

dist/types.d.ts:1582


Headers

Headers: Object

Type declaration

Name Type
prototype Headers

Defined in

globals.d.ts:271

globals.d.ts:286

dist/types.d.ts:682

dist/types.d.ts:697


Loader

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.

Type declaration

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.

Defined in

globals.d.ts:1583

dist/types.d.ts:1994


MessageEvent

MessageEvent: Object

Type declaration

Name Type
prototype MessageEvent<any>

Defined in

globals.d.ts:1316

globals.d.ts:1336

dist/types.d.ts:1727

dist/types.d.ts:1747


ReadableStream

ReadableStream: Object

Type declaration

Name Type
prototype ReadableStream<any>

Defined in

globals.d.ts:1673

globals.d.ts:1692

dist/types.d.ts:2084

dist/types.d.ts:2103


ReadableStreamDefaultController

ReadableStreamDefaultController: Object

Type declaration

Name Type
prototype ReadableStreamDefaultController<any>

Defined in

globals.d.ts:1725

globals.d.ts:1740

dist/types.d.ts:2136

dist/types.d.ts:2151


ReadableStreamDefaultReader

ReadableStreamDefaultReader: Object

Type declaration

Name Type
prototype ReadableStreamDefaultReader<any>

Defined in

globals.d.ts:1745

globals.d.ts:1751

dist/types.d.ts:2156

dist/types.d.ts:2162


TransformStream

TransformStream: Object

Type declaration

Name Type
prototype TransformStream<any, any>

Defined in

globals.d.ts:1900

globals.d.ts:1905

dist/types.d.ts:2311

dist/types.d.ts:2316


TransformStreamDefaultController

TransformStreamDefaultController: Object

Type declaration

Name Type
prototype TransformStreamDefaultController<any>

Defined in

globals.d.ts:1914

globals.d.ts:1921

dist/types.d.ts:2325

dist/types.d.ts:2332


WebSocket

WebSocket: Object

Type declaration

Name Type
CLOSED number
CLOSING number
CONNECTING number
OPEN number
prototype WebSocket

Defined in

globals.d.ts:1352

globals.d.ts:1407

dist/types.d.ts:1763

dist/types.d.ts:1818


WritableStream

WritableStream: Object

Type declaration

Name Type
prototype WritableStream<any>

Defined in

globals.d.ts:1782

globals.d.ts:1789

dist/types.d.ts:2193

dist/types.d.ts:2200


WritableStreamDefaultController

WritableStreamDefaultController: Object

Type declaration

Name Type
prototype WritableStreamDefaultController

Defined in

globals.d.ts:1798

globals.d.ts:1802

dist/types.d.ts:2209

dist/types.d.ts:2213


WritableStreamDefaultWriter

WritableStreamDefaultWriter: Object

Type declaration

Name Type
prototype WritableStreamDefaultWriter<any>

Defined in

globals.d.ts:1808

globals.d.ts:1818

dist/types.d.ts:2219

dist/types.d.ts:2229


crypto

crypto: Crypto

Defined in

globals.d.ts:754

dist/types.d.ts:1165


performance

performance: Object

Type declaration

Name Type
now () => number

Defined in

globals.d.ts:952

dist/types.d.ts:1363

Functions

addEventListener

addEventListener<K>(type, listener, options?): void

Type parameters

Name Type
K extends "fetch"

Parameters

Name Type
type K
listener (this: object, ev: EventMap[K]) => any
options? boolean | AddEventListenerOptions

Returns

void

Defined in

globals.d.ts:1028

addEventListener(type, listener, options?): void

Parameters

Name Type
type string
listener EventListenerOrEventListenerObject
options? boolean | AddEventListenerOptions

Returns

void

Defined in

globals.d.ts:1033

addEventListener<K>(type, listener, options?): void

Type parameters

Name Type
K extends "fetch"

Parameters

Name Type
type K
listener (this: object, ev: EventMap[K]) => any
options? boolean | AddEventListenerOptions

Returns

void

Defined in

dist/types.d.ts:1439

addEventListener(type, listener, options?): void

Parameters

Name Type
type string
listener EventListenerOrEventListenerObject
options? boolean | AddEventListenerOptions

Returns

void

Defined in

dist/types.d.ts:1444


alert

alert(message?): void

Parameters

Name Type
message? string

Returns

void

Defined in

globals.d.ts:1988

alert(message?): void

Parameters

Name Type
message? string

Returns

void

Defined in

dist/types.d.ts:2399


atob

atob(encodedData): string

atob decodes base64 into ascii text.

Parameters

Name Type
encodedData string

Returns

string

Defined in

globals.d.ts:761

atob(encodedData): string

atob decodes base64 into ascii text.

Parameters

Name Type
encodedData string

Returns

string

Defined in

dist/types.d.ts:1172


btoa

btoa(stringToEncode): string

btoa encodes ascii text into base64.

Parameters

Name Type Description
stringToEncode string The ascii text to encode.

Returns

string

Defined in

globals.d.ts:768

btoa(stringToEncode): string

btoa encodes ascii text into base64.

Parameters

Name Type Description
stringToEncode string The ascii text to encode.

Returns

string

Defined in

dist/types.d.ts:1179


confirm

confirm(message?): boolean

Parameters

Name Type
message? string

Returns

boolean

Defined in

globals.d.ts:1989

confirm(message?): boolean

Parameters

Name Type
message? string

Returns

boolean

Defined in

dist/types.d.ts:2400


fetch

fetch(url, init?): Promise<Response>

Send a HTTP(s) request

Parameters

Name Type Description
url string URL string
init? RequestInit A structured value that contains settings for the fetch() request.

Returns

Promise<Response>

A promise that resolves to Response object.

Defined in

globals.d.ts:987

fetch(request, init?): Promise<Response>

Send a HTTP(s) request

Parameters

Name Type Description
request Request Request object
init? RequestInit A structured value that contains settings for the fetch() request.

Returns

Promise<Response>

A promise that resolves to Response object.

Defined in

globals.d.ts:1000

fetch(url, init?): Promise<Response>

Send a HTTP(s) request

Parameters

Name Type Description
url string URL string
init? RequestInit A structured value that contains settings for the fetch() request.

Returns

Promise<Response>

A promise that resolves to Response object.

Defined in

dist/types.d.ts:1398

fetch(request, init?): Promise<Response>

Send a HTTP(s) request

Parameters

Name Type Description
request Request Request object
init? RequestInit A structured value that contains settings for the fetch() request.

Returns

Promise<Response>

A promise that resolves to Response object.

Defined in

dist/types.d.ts:1411


prompt

prompt(message?, _default?): string | null

Parameters

Name Type
message? string
_default? string

Returns

string | null

Defined in

globals.d.ts:1990

prompt(message?, _default?): string | null

Parameters

Name Type
message? string
_default? string

Returns

string | null

Defined in

dist/types.d.ts:2401


removeEventListener

removeEventListener<K>(type, listener, options?): void

Type parameters

Name Type
K extends "fetch"

Parameters

Name Type
type K
listener (this: object, ev: EventMap[K]) => any
options? boolean | EventListenerOptions

Returns

void

Defined in

globals.d.ts:1038

removeEventListener(type, listener, options?): void

Parameters

Name Type
type string
listener EventListenerOrEventListenerObject
options? boolean | EventListenerOptions

Returns

void

Defined in

globals.d.ts:1043

removeEventListener<K>(type, listener, options?): void

Type parameters

Name Type
K extends "fetch"

Parameters

Name Type
type K
listener (this: object, ev: EventMap[K]) => any
options? boolean | EventListenerOptions

Returns

void

Defined in

dist/types.d.ts:1449

removeEventListener(type, listener, options?): void

Parameters

Name Type
type string
listener EventListenerOrEventListenerObject
options? boolean | EventListenerOptions

Returns

void

Defined in

dist/types.d.ts:1454


reportError

reportError(error): void

Log an error using the default exception handler

Parameters

Name Type Description
error any Error or string

Returns

void

Defined in

globals.d.ts:1007

reportError(error): void

Log an error using the default exception handler

Parameters

Name Type Description
error any Error or string

Returns

void

Defined in

dist/types.d.ts:1418

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