Begin some work on more-correct types

This commit is contained in:
Alistair Smith
2024-11-22 18:00:49 -08:00
parent 804569cf2a
commit 446fa31c19
11 changed files with 111 additions and 89 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -16,6 +16,7 @@
declare module "bun" {
import type { Encoding as CryptoEncoding } from "crypto";
import type { CipherNameAndProtocol, EphemeralKeyInfo, PeerCertificate } from "tls";
interface Env {
NODE_ENV?: string;
/**
@@ -163,6 +164,19 @@ declare module "bun" {
*/
arrayBuffer(): ArrayBuffer;
/**
* Read from stdout as a Uint8Array
*
* @returns Stdout as a Uint8Array
* @example
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.bytes()); // Uint8Array { byteLength: 6 }
* ```
*/
bytes(): Uint8Array;
/**
* Read from stdout as a Blob
*
@@ -411,9 +425,9 @@ declare module "bun" {
arrayBuffer(): ArrayBuffer;
/**
* Read from stdout as an Uint8Array
* Read from stdout as a Uint8Array
*
* @returns Stdout as an Uint8Array
* @returns Stdout as a Uint8Array
* @example
*
* ```ts

View File

@@ -3,30 +3,30 @@ declare module "bun" {
* @deprecated Renamed to `ErrorLike`
*/
type Errorlike = ErrorLike;
interface TLSOptions {
/**
* File path to a TLS key
*
* To enable TLS, this option is required.
*
* @deprecated since v0.6.3 - Use `key: Bun.file(path)` instead.
*/
keyFile?: string;
/**
* File path to a TLS certificate
*
* To enable TLS, this option is required.
*
* @deprecated since v0.6.3 - Use `cert: Bun.file(path)` instead.
*/
certFile?: string;
/**
* File path to a .pem file for a custom root CA
*
* @deprecated since v0.6.3 - Use `ca: Bun.file(path)` instead.
*/
caFile?: string;
}
// interface TLSOptions {
// /**
// * File path to a TLS key
// *
// * To enable TLS, this option is required.
// *
// * @deprecated since v0.6.3 - Use `key: Bun.file(path)` instead.
// */
// keyFile?: string;
// /**
// * File path to a TLS certificate
// *
// * To enable TLS, this option is required.
// *
// * @deprecated since v0.6.3 - Use `cert: Bun.file(path)` instead.
// */
// certFile?: string;
// /**
// * File path to a .pem file for a custom root CA
// *
// * @deprecated since v0.6.3 - Use `ca: Bun.file(path)` instead.
// */
// caFile?: string;
// }
}
declare namespace NodeJS {

View File

@@ -1,6 +1,3 @@
type _Response = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Response;
export interface Response extends _Response {}
export declare class Response {
constructor(body?: Bun.BodyInit | null | undefined, init?: Bun.ResponseInit | undefined);
@@ -51,3 +48,9 @@ export declare class Response {
*/
static error(): Response;
}
export declare class Request {
constructor(...params: Parameters<typeof fetch>);
// TODO: Rest of types
}

View File

@@ -6,6 +6,14 @@ type _ReadableStream<T> = typeof globalThis extends {
}
? T
: import("stream/web").ReadableStream<T>;
type _RequestInit = typeof globalThis extends {
onerror: any;
RequestInit: infer T;
}
? T
: NonNullable<ConstructorParameters<typeof import("undici-fetch").Request>[1]>;
type _WritableStream<T> = typeof globalThis extends {
onerror: any;
WritableStream: infer T;
@@ -141,17 +149,9 @@ import type { TextDecoder as NodeTextDecoder, TextEncoder as NodeTextEncoder } f
import type { MessagePort } from "worker_threads";
import type { WebSocket as _WebSocket } from "ws";
declare module "*.txt" {
var text: string;
export = text;
}
declare module "*.toml" {
var contents: any;
export = contents;
}
declare global {
var onmessage: never; // Not a fan of this - required so Node.js' globals.d.ts doesn't conflict with our own definitions. Not a perfect solution though.
var Bun: typeof import("bun");
namespace NodeJS {
@@ -804,9 +804,10 @@ declare global {
readonly lastModified: number;
readonly name: string;
}
var File: typeof globalThis extends { onerror: any; File: infer T } ? T : typeof File;
interface FetchRequestInit extends RequestInit {
var File: File;
interface RequestInit extends _RequestInit {
/**
* Log the raw HTTP request & response to stdout. This API may be
* removed in a future version of Bun without notice.
@@ -823,10 +824,7 @@ declare global {
/**
* Override the default TLS options
*/
tls?: {
rejectUnauthorized?: boolean | undefined; // Defaults to true
checkServerIdentity?: any; // TODO: change `any` to `checkServerIdentity`
};
tls?: import("bun").TLSOptions;
}
/**
@@ -915,29 +913,8 @@ declare global {
new (): ShadowRealm;
};
interface Fetch {
/**
* Send a HTTP(s) request
*
* @param request Request object
* @param init A structured value that contains settings for the fetch() request.
*
* @returns A promise that resolves to {@link Response} object.
*/
(request: Request, init?: RequestInit): Promise<Response>;
/**
* Send a HTTP(s) request
*
* @param url URL string
* @param init A structured value that contains settings for the fetch() request.
*
* @returns A promise that resolves to {@link Response} object.
*/
(url: string | URL | Request, init?: FetchRequestInit): Promise<Response>;
(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>;
// @types/node defines fetch, we can merge our `.preconnect(url)` 'static' method
namespace fetch {
/**
* Start the DNS resolution, TCP connection, and TLS handshake for a request
* before the request is actually sent.
@@ -946,13 +923,14 @@ declare global {
* long-running task that will delay the request starting.
*
* This is a bun-specific API and is not part of the Fetch API specification.
*
* @param url A string or URL instance to preconnect
*/
preconnect(url: string | URL): void;
export function preconnect(url: string | URL): void;
}
var fetch: Fetch;
function queueMicrotask(callback: (...args: any[]) => void): void;
/**
* Log an error using the default exception handler
* @param error Error or string
@@ -1835,10 +1813,10 @@ declare global {
readonly main: boolean;
/** Alias of `import.meta.dir`. Exists for Node.js compatibility */
readonly dirname: string;
/* readonly */ dirname: string; // Ideally this would be `readonly`, but Node.js doesn't mark it readonly so TypeScript tells us off
/** Alias of `import.meta.path`. Exists for Node.js compatibility */
readonly filename: string;
/* readonly */ filename: string; // Ideally this would be `readonly`, but Node.js doesn't mark it readonly so TypeScript tells us off
}
/**
@@ -1969,17 +1947,17 @@ declare global {
? T
: typeof import("./fetch").Response;
var Request: typeof globalThis extends {
onerror: any;
Request: infer T;
}
? T
: {
prototype: Request;
new (requestInfo: string, requestInit?: RequestInit): Request;
new (requestInfo: RequestInit & { url: string }): Request;
new (requestInfo: Request, requestInit?: RequestInit): Request;
};
// var Request: typeof globalThis extends {
// onerror: any;
// Request: infer T;
// }
// ? T
// : {
// prototype: Request;
// new (requestInfo: string, requestInit?: RequestInit): Request;
// new (requestInfo: RequestInit & { url: string }): Request;
// new (requestInfo: Request, requestInit?: RequestInit): Request;
// };
interface Headers {
/**

View File

@@ -2,6 +2,8 @@
// Definitions by: Jarred Sumner <https://github.com/Jarred-Sumner>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference types="node" />
/// <reference types="ws" />
@@ -10,6 +12,7 @@
//// <reference lib="dom" />
/// <reference path="./globals.d.ts" />
/// <reference path="./wildcards.d.ts" />
/// <reference path="./bun.d.ts" />
/// <reference path="./overrides.d.ts" />
/// <reference path="./fetch.d.ts" />

View File

@@ -14,7 +14,8 @@
"homepage": "https://bun.sh",
"dependencies": {
"@types/node": "~20.12.8",
"@types/ws": "~8.5.10"
"@types/ws": "~8.5.10",
"undici-fetch": "^1.0.0-rc.4"
},
"devDependencies": {
"@biomejs/biome": "^1.5.3",

View File

@@ -0,0 +1,12 @@
fetch.preconnect("bun");
fetch("bun");
fetch("bun", {
verbose: true,
});
const init: RequestInit = {
proxy: "12345",
verbose: true,
method: "GET",
};

View File

@@ -1,4 +0,0 @@
import data from "./bunfig.toml";
import { expectAny } from "./utilities.test";
expectAny(data);

View File

@@ -0,0 +1,6 @@
import toml from "./bunfig.toml";
import text from "./bunfig.txt";
import { expectAny, expectType } from "./utilities.test";
expectAny(toml);
expectType<string>(text);

9
packages/bun-types/wildcards.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
declare module "*.txt" {
var text: string;
export = text;
}
declare module "*.toml" {
var contents: any;
export = contents;
}