Compare commits

...

2 Commits

Author SHA1 Message Date
Alistair Smith
446fa31c19 Begin some work on more-correct types 2024-11-22 18:00:49 -08:00
Alistair Smith
804569cf2a some work on improving types & repo structure 2024-11-22 16:44:30 -08:00
20 changed files with 122 additions and 171 deletions

View File

@@ -1,8 +0,0 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": ".",
"jsx": "react-jsx",
"paths": {}
}
}

View File

@@ -1,4 +0,0 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {}
}

BIN
bun.lockb

Binary file not shown.

View File

@@ -1,22 +0,0 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"importsNotUsedAsValues": "preserve"
},
"include": [".", "packages/bun-types/index.d.ts"],
"exclude": [
"src/test",
"src/js/out",
// "src/js/builtins",
"packages",
"bench",
"examples/*/*",
"test",
"vendor",
"bun-webkit",
"vendor/WebKit",
"src/api/demo",
"node_modules"
],
"files": ["src/js/builtins.d.ts"]
}

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

@@ -1,7 +1,6 @@
{
"name": "bun-types",
"license": "MIT",
"main": "",
"types": "index.d.ts",
"description": "Type definitions for Bun, an incredibly fast JavaScript runtime",
"repository": {
@@ -15,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

@@ -5,6 +5,7 @@ const rl = readline.createInterface({
output: process.stdout,
terminal: true,
});
await rl.question("What is your age?\n").then(answer => {
console.log("Your age is: " + answer);
});

View File

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

View File

@@ -6,3 +6,10 @@ export declare const expectAssignable: <T>(expression: T) => void;
export declare const expectNotAssignable: <T>(expression: any) => void;
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
export declare const expectTypeEquals: <T, S>(expression: T extends S ? (S extends T ? true : false) : false) => void;
type IsAny<T> = 0 extends 1 & T ? true : false;
type IsNever<T> = [T] extends [never] ? true : false;
type OnlyAny<T> = IsNever<T> extends true ? [] : IsAny<T> extends true ? [T] : never;
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
export declare function expectAny<T>(...value: OnlyAny<T>): void;

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);

View File

@@ -10,7 +10,9 @@
"disableSolutionSearching": true,
"noUnusedLocals": true,
"noEmit": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"types": ["./index.d.ts"]
},
"include": ["./**/*.ts", "./**/*.d.ts"],
"exclude": ["dist", "node_modules"]
}

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;
}

View File

@@ -1,5 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"module": "ESNext",

View File

@@ -1,19 +0,0 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "Bundler",
"allowImportingTsExtensions": true,
"noEmit": true,
"strict": true,
"noImplicitAny": false,
"allowJs": true,
"downlevelIteration": true,
"esModuleInterop": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"typeRoots": ["./packages"]
}
}

View File

@@ -1,26 +0,0 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
// "skipLibCheck": true,
"allowJs": true
},
"include": [".", "packages/bun-types/index.d.ts"],
"exclude": [
"src/test",
"src/js/out",
// "src/js/builtins",
"packages",
"bench",
"examples/*/*",
"build",
".zig-cache",
"test",
"vendor",
"bun-webkit",
"src/api/demo",
"node_modules"
],
"files": ["src/js/builtins.d.ts"]
}