diff options
author | 2023-03-22 15:01:01 -0700 | |
---|---|---|
committer | 2023-03-22 15:01:01 -0700 | |
commit | a5f92224b586289fc72f0abdb68b08eef9f017db (patch) | |
tree | 6092397858776820b431b0dffa635d8bc3b3185e /packages | |
parent | 2bdaa81b1c2325687c5115b4e97627533cb3646b (diff) | |
download | bun-a5f92224b586289fc72f0abdb68b08eef9f017db.tar.gz bun-a5f92224b586289fc72f0abdb68b08eef9f017db.tar.zst bun-a5f92224b586289fc72f0abdb68b08eef9f017db.zip |
Fix types (#2453)
* WIP
* WIP
* WIP
* WIP
* Improve typechecking in type files
* Fix typechecking
* Update
* Update submodule
* CI for typechecking
* Add ci
* Update commands
* Format after build
* Dont use bunx
* Rename job
* Use nodemodules prettier
* Update workflow
* Use symlink
* Debug
* Debug
* Clean up and rename jobs
Diffstat (limited to 'packages')
-rw-r--r-- | packages/bun-types/bun-test.d.ts | 29 | ||||
-rw-r--r-- | packages/bun-types/bun.d.ts | 11 | ||||
-rwxr-xr-x | packages/bun-types/bun.lockb | bin | 38313 -> 38699 bytes | |||
-rw-r--r-- | packages/bun-types/globals.d.ts | 38 | ||||
-rw-r--r-- | packages/bun-types/package.json | 20 | ||||
-rw-r--r-- | packages/bun-types/tests/ffi.test-d.ts | 2 | ||||
-rw-r--r-- | packages/bun-types/tests/globals.test-d.ts | 6 | ||||
-rw-r--r-- | packages/bun-types/tests/http.test-d.ts | 6 |
8 files changed, 80 insertions, 32 deletions
diff --git a/packages/bun-types/bun-test.d.ts b/packages/bun-types/bun-test.d.ts index 16b4cf90c..181d4c79d 100644 --- a/packages/bun-types/bun-test.d.ts +++ b/packages/bun-types/bun-test.d.ts @@ -199,7 +199,12 @@ declare module "bun:test" { * * @param actual the actual value */ - export function expect(actual: unknown): Expect; + export const expect: { + (actual: unknown): Expect; + any: ( + constructor: ((..._: any[]) => any) | { new (..._: any[]): any }, + ) => Expect; + }; /** * Asserts that a value matches some criteria. * @@ -211,7 +216,7 @@ declare module "bun:test" { * * @param actual the actual value */ - export type Expect = { + export type Expect<T = unknown> = { /** * Negates the result of a subsequent assertion. * @@ -219,7 +224,7 @@ declare module "bun:test" { * expect(1).not.toBe(0); * expect(null).not.toBeNull(); */ - not: Expect; + not: Expect<unknown>; /** * Asserts that a value equals what is expected. * @@ -235,7 +240,7 @@ declare module "bun:test" { * * @param expected the expected value */ - toBe(expected: unknown): void; + toBe(expected: T): void; /** * Asserts that a value is deeply equal to what is expected. * @@ -247,7 +252,7 @@ declare module "bun:test" { * * @param expected the expected value */ - toEqual(expected: unknown): void; + toEqual(expected: T): void; /** * Asserts that a value is deeply and strictly equal to * what is expected. @@ -271,7 +276,7 @@ declare module "bun:test" { * * @param expected the expected value */ - toStrictEqual(expected: unknown): void; + toStrictEqual(expected: T): void; /** * Asserts that a value contains what is expected. * @@ -341,7 +346,15 @@ declare module "bun:test" { * expect(undefined).toBeDefined(); // fail */ toBeDefined(): void; - /** + /** + * Asserts that the expected value is an instance of value + * + * @example + * expect([]).toBeInstanceOf(Array); + * expect(null).toBeInstanceOf(Array); // fail + */ + toBeInstanceOf(value: unknown): void; + /** * Asserts that the expected value is an instance of value * * @example @@ -462,7 +475,7 @@ declare module "bun:test" { * @param hint Hint used to identify the snapshot in the snapshot file. */ toMatchSnapshot(propertyMatchers?: Object, hint?: string): void; - } + }; } declare module "test" { diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index 3773d3ebb..1768432df 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -500,7 +500,7 @@ declare module "bun" { * `"system"` uses the same API underneath (except non-blocking). * */ - backend?: "c-ares" | "system" | "getaddrinfo"; + backend?: "libc" | "c-ares" | "system" | "getaddrinfo"; }, ): Promise<DNSLookup[]>; }; @@ -1492,6 +1492,7 @@ declare module "bun" { ) => Response | Promise<Response> | undefined | void | Promise<undefined>; } + export type AnyFunction = (..._: any[]) => any; export interface ServeOptions extends GenericServeOptions { /** * Handle HTTP requests @@ -2819,7 +2820,7 @@ declare module "bun" { reload(options: Pick<Partial<SocketOptions>, "socket">): void; data: Data; } - interface TCPSocketListener<Data> extends SocketListener<Data> { + interface TCPSocketListener<Data = unknown> extends SocketListener<Data> { readonly port: number; readonly hostname: string; } @@ -3170,6 +3171,8 @@ declare module "bun" { /** The base path to use when routing */ assetPrefix?: string; origin?: string; + /** Limit the pages to those with particular file extensions. */ + fileExtensions?: string[]; }); // todo: URL @@ -3365,7 +3368,9 @@ type TypedArray = | Int32Array | Uint32Array | Float32Array - | Float64Array; + | Float64Array + | BigInt64Array + | BigUint64Array; type TimeLike = string | number | Date; type StringOrBuffer = string | TypedArray | ArrayBufferLike; diff --git a/packages/bun-types/bun.lockb b/packages/bun-types/bun.lockb Binary files differindex e764f6a0c..066d286b9 100755 --- a/packages/bun-types/bun.lockb +++ b/packages/bun-types/bun.lockb diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index 82f8cfb8b..6ff2e9970 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -417,7 +417,7 @@ interface BlobInterface { formData(): Promise<FormData>; } -type BlobPart = string | Blob | BufferSource | ArrayBuffer; +type BlobPart = string | Blob | BufferSource; interface BlobPropertyBag { /** Set a default "type" */ type?: string; @@ -639,7 +639,7 @@ declare class Blob implements BlobInterface { interface ResponseInit { headers?: HeadersInit; /** @default 200 */ - status?: number; + status?: number | bigint; /** @default "OK" */ statusText?: string; @@ -663,7 +663,13 @@ interface ResponseInit { */ declare class Response implements BlobInterface { constructor( - body?: ReadableStream | BlobPart | BlobPart[] | null | FormData, + body?: + | ReadableStream + | BlobPart + | BlobPart[] + | FormData + | URLSearchParams + | null, options?: ResponseInit, ); @@ -851,9 +857,9 @@ type ReferrerPolicy = | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url"; -type RequestInfo = Request | string | RequestInit; +// type RequestInfo = Request | string | RequestInit; -type BodyInit = ReadableStream | XMLHttpRequestBodyInit; +type BodyInit = ReadableStream | XMLHttpRequestBodyInit | URLSearchParams; type XMLHttpRequestBodyInit = Blob | BufferSource | string | FormData; type ReadableStreamController<T> = ReadableStreamDefaultController<T>; type ReadableStreamDefaultReadResult<T> = @@ -962,7 +968,10 @@ interface FetchRequestInit extends RequestInit { * ``` */ declare class Request implements BlobInterface { - constructor(requestInfo: RequestInfo, requestInit?: RequestInit); + // Request | string | RequestInit; + constructor(requestInfo: string, requestInit?: RequestInit); + constructor(requestInfo: RequestInit & { url: string }); + constructor(requestInfo: Request, requestInit?: RequestInit); /** * Read or write the HTTP headers for this request. @@ -1375,7 +1384,7 @@ declare function clearImmediate(id?: number | Timer): void; */ declare function fetch( - url: string | URL, + url: string | URL | Request, init?: FetchRequestInit, ): Promise<Response>; @@ -1584,7 +1593,7 @@ declare var EventTarget: { }; /** An event which takes place in the DOM. */ -interface Event { +interface Event<T extends EventTarget = EventTarget> { /** * Returns true or false depending on how event was initialized. True * if event goes through its target's ancestors in reverse tree order, @@ -1609,7 +1618,7 @@ interface Event { * Returns the object whose event listener's callback is currently * being invoked. */ - readonly currentTarget: EventTarget | null; + readonly currentTarget: T | null; /** * Returns true if preventDefault() was invoked successfully to * indicate cancelation, and false otherwise. @@ -1909,6 +1918,7 @@ interface URLSearchParams { ): void; /** Returns a string containing a query string suitable for use in a URL. Does not include the question mark. */ toString(): string; + [Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>; } declare var URLSearchParams: { @@ -1957,7 +1967,7 @@ interface EventMap { } interface AbortSignalEventMap { - abort: Event; + abort: Event<AbortSignal>; } interface AddEventListenerOptions extends EventListenerOptions { @@ -1972,10 +1982,12 @@ interface AbortSignal extends EventTarget { * Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise. */ readonly aborted: boolean; + /** * The reason the signal aborted, or undefined if not aborted. */ readonly reason: any; + onabort: ((this: AbortSignal, ev: Event) => any) | null; addEventListener<K extends keyof AbortSignalEventMap>( type: K, @@ -2157,7 +2169,11 @@ interface ReadableStream<R = any> { declare var ReadableStream: { prototype: ReadableStream; new <R = any>( - underlyingSource?: DirectUnderlyingSource<R> | UnderlyingSource<R>, + underlyingSource?: UnderlyingSource<R>, + strategy?: QueuingStrategy<R>, + ): ReadableStream<R>; + new <R = any>( + underlyingSource?: DirectUnderlyingSource<R>, strategy?: QueuingStrategy<R>, ): ReadableStream<R>; }; diff --git a/packages/bun-types/package.json b/packages/bun-types/package.json index 2dbf1839d..059ab0bfc 100644 --- a/packages/bun-types/package.json +++ b/packages/bun-types/package.json @@ -1,20 +1,22 @@ { "name": "bun-types", - "types": "index.d.ts", - "private": true, "repository": "https://github.com/oven-sh/bun", + "devDependencies": { + "conditional-type-checks": "^1.0.6", + "prettier": "^2.4.1", + "tsd": "^0.22.0", + "typescript": "^5.0.2" + }, + "private": true, "scripts": { + "prebuild": "echo $(pwd)", "build": "rm -rf ./dist && bun run bundle && bun run fmt", "bundle": "bun scripts/bundle.ts ./dist", "test": "tsd", - "fmt": "prettier --write './**/*.{ts,tsx,js,jsx}'" - }, - "devDependencies": { - "conditional-type-checks": "^1.0.6", - "prettier": "^2.4.1", - "tsd": "^0.22.0" + "fmt": "echo $(which prettier) && prettier --write './**/*.{ts,tsx,js,jsx}'" }, "tsd": { "directory": "tests" - } + }, + "types": "index.d.ts" } diff --git a/packages/bun-types/tests/ffi.test-d.ts b/packages/bun-types/tests/ffi.test-d.ts index 105736134..43088c679 100644 --- a/packages/bun-types/tests/ffi.test-d.ts +++ b/packages/bun-types/tests/ffi.test-d.ts @@ -60,7 +60,7 @@ tsd.expectType<number>(lib.symbols.add(1, 2)); tc.assert< tc.IsExact< - typeof lib["symbols"]["allArgs"], + (typeof lib)["symbols"]["allArgs"], [ number, number, diff --git a/packages/bun-types/tests/globals.test-d.ts b/packages/bun-types/tests/globals.test-d.ts index c67aa4256..64b37be13 100644 --- a/packages/bun-types/tests/globals.test-d.ts +++ b/packages/bun-types/tests/globals.test-d.ts @@ -87,3 +87,9 @@ global.Bun; const er = new DOMException(); er.name; er.HIERARCHY_REQUEST_ERR; + +new Request(new Request("https://example.com"), {}); +new Request("", { method: "POST" }); + +Bun.sleepSync(1); // sleep for 1 ms (not recommended) +await Bun.sleep(1); // sleep for 1 ms (recommended) diff --git a/packages/bun-types/tests/http.test-d.ts b/packages/bun-types/tests/http.test-d.ts index d9754ac0c..aee0b3703 100644 --- a/packages/bun-types/tests/http.test-d.ts +++ b/packages/bun-types/tests/http.test-d.ts @@ -25,3 +25,9 @@ const req = http.request({ host: "localhost", port: 3000, method: "GET" }); req.abort; req.end(); export {}; + +// URLSearchParams should be iterable +const sp = new URLSearchParams("q=foo&bar=baz"); +for (const q of sp) { + console.log(q); +} |