Skip to content

Commit ad59836

Browse files
committed
TS: update type definitions to support TS v6
Types can remain common with existing v5.5+ ones, since only `ReadableStreamBYOBReader.read` was updated in a backwards-compatible way. The tests needed some changes, specifically isolating browser ones with a standalone tsconfig, due to type resolution/erasure changes in v6 following the inclusion of the DOM.AsyncIterable lib in the main DOM types (with the change, the ReadableStream Node types took precedence over the DOM ones). With this fix, the browser tests no longer bring in Node types, hence the ReadableStream type resolution should remain deterministic from now on.
1 parent 4fc1624 commit ad59836

8 files changed

Lines changed: 35 additions & 7 deletions

File tree

lib/types/dom.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export namespace DOM {
5858
*/
5959
getWriter(): WritableStreamDefaultWriter<W>;
6060
}
61+
interface ReadableStreamBYOBReaderReadOptions {
62+
min?: number;
63+
}
6164
/**
6265
* The `ReadableStream` interface of the Streams API represents a readable stream of byte data.
6366
*
@@ -126,7 +129,12 @@ export namespace DOM {
126129
*
127130
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/read)
128131
*/
129-
read<T extends ArrayBufferView>(view: T): Promise<globalThis.ReadableStreamReadResult<T>>;
132+
read<T extends Exclude<BufferSource, ArrayBuffer>>(
133+
view: T,
134+
// `options` are included in TS v6 definitions but not in v5; to keep the types common
135+
// we redeclare `ReadableStreamBYOBReaderReadOptions`
136+
options?: ReadableStreamBYOBReaderReadOptions
137+
): Promise<globalThis.ReadableStreamReadResult<T>>;
130138
/**
131139
* The **`releaseLock()`** method of the ReadableStreamBYOBReader interface releases the reader's lock on the stream.
132140
*

test/browser/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true, // code tested by tsx
5+
"types": ["mocha", "chai"], // no node types
6+
"lib": [] // DOM.AsyncIterable is included in the main DOM types in v6
7+
},
8+
}

test/common.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { expect } from 'chai';
2-
// @ts-expect-error Missing type definitions
3-
import { toStream, toArrayStream, readToEnd, slice, pipe, ArrayStream, transform, transformAsync, type Stream } from '@openpgp/web-stream-tools';
2+
import {
3+
toStream,
4+
// @ts-expect-error Missing type definitions
5+
toArrayStream, pipe, ArrayStream,
6+
readToEnd,
7+
slice,
8+
transform,
9+
transformAsync,
10+
type Stream
11+
} from '@openpgp/web-stream-tools';
412

513
describe('Common integration tests', () => {
614
it('toStream/readToEnd', async () => {

test/tsconfig.es5.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"target": "es5",
5-
"noEmit": true, // code tested by tsx
6-
"lib": ["DOM.AsyncIterable"]
5+
// Option 'target=ES5' is deprecated and will stop functioning in TypeScript 7.0
6+
// Specify compilerOption '"ignoreDeprecations": "6.0"' to silence this error.
7+
"ignoreDeprecations": "6.0"
78
}
89
}

test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
44
"noEmit": true, // code tested by tsx
5-
"lib": ["DOM.AsyncIterable"], // for browser tests (for-await)
5+
"types": ["node", "mocha", "chai"]
66
},
77
}

test/tsconfig.v4.7.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
44
"noEmit": true, // code tested by tsx
5+
"skipLibCheck": true
56
},
67
"include": ["./typescript.v4.7.test.ts"]
78
}

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
"allowJs": true,
1010
"paths": {
1111
"@openpgp/web-stream-tools": ["."]
12-
}
12+
},
13+
"noEmit": true
1314
},
1415
"exclude": [
1516
"node_modules",
17+
"docs"
1618
],
1719
}

0 commit comments

Comments
 (0)