Skip to content

Commit c6c47e0

Browse files
committed
Types stabilization for TS <= 5.5
1 parent cae4fb7 commit c6c47e0

File tree

7 files changed

+135
-10
lines changed

7 files changed

+135
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="../pure/web/url.d.ts" />
2+
3+
declare namespace CoreJS {
4+
export type URLParse = (url: string | CoreJSURL, base?: string | CoreJSURL) => CoreJSURL | null;
5+
}

packages/core-js-types/src/ts5-2/core-js-types/core-js-types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
declare global {
77
interface IteratorObject<T, TReturn = any, TNext = any> extends Iterator<T, TReturn, TNext> {}
88

9-
interface AsyncIteratorObject<T, TReturn = any, TNext = any> extends AsyncIterator<T> {
9+
interface AsyncIteratorObject<T, TReturn = any, TNext = undefined> extends AsyncIterator<T> {
1010
[Symbol.asyncIterator](): AsyncIteratorObject<T, TReturn, TNext>;
1111
}
1212

@@ -21,7 +21,7 @@ declare global {
2121
[Symbol.asyncIterator](): AsyncGenerator<T, TReturn, TNext>;
2222
}
2323

24-
interface AsyncIterator<T, TReturn = any, TNext = any> {
24+
interface AsyncIterator<T, TReturn = any, TNext = undefined> {
2525
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
2626
next(...[value]: [] | [TNext]): Promise<IteratorResult<T, TReturn>>;
2727
return?(value?: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T, TReturn>>;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// https://github.com/tc39/proposal-async-iterator-helpers
2+
3+
interface AsyncIteratorConstructor {
4+
/**
5+
* Creates an `AsyncIterator` from an iterable object
6+
* @param iterable An `AsyncIterable`, `Iterable`, or `AsyncIterator` to convert to an `AsyncIterator`
7+
* @returns A new `AsyncIterator` instance
8+
*/
9+
from<T>(iterable: AsyncIterable<T> | Iterable<T> | AsyncIteratorObject<T>): AsyncIteratorObject<T>;
10+
}
11+
12+
declare var AsyncIterator: AsyncIteratorConstructor;
13+
14+
interface AsyncIterator<T, TReturn = any, TNext = undefined> {
15+
/**
16+
* Drops elements from the iterator until the limit is reached
17+
* @param limit The number of elements to drop
18+
* @returns A new `AsyncIterator`
19+
*/
20+
drop(limit: number): AsyncIteratorObject<T>;
21+
22+
/**
23+
* Check if every value generated by the iterator passes the `predicate` function.
24+
* @param predicate A function that tests each element of the iterator
25+
* @returns A promise that resolves to `true` if all elements pass the test, otherwise `false`
26+
*/
27+
every(predicate: (value: T, index: number) => boolean): Promise<boolean>;
28+
29+
/**
30+
* Creates a new `AsyncIterator` that contains only the elements that pass the `predicate` function.
31+
* @param predicate A function that tests each element of the iterator
32+
* @returns A new `AsyncIterator`
33+
*/
34+
filter(predicate: (value: T, index: number) => boolean): AsyncIteratorObject<T>;
35+
36+
/**
37+
* Finds the first element in the iterator that satisfies the `predicate` function.
38+
* @param predicate A function that tests each element of the iterator
39+
* @returns A `Promise` that resolves to the found element, or `undefined` if no element satisfies the `predicate`
40+
*/
41+
find(predicate: (value: T, index: number) => boolean): Promise<T>;
42+
43+
/**
44+
* Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator and flattening the result.
45+
* @param mapper A function that transforms each element of the iterator
46+
* @returns A new `AsyncIterator`
47+
*/
48+
flatMap(mapper: (value: T, index: number) => any): AsyncIteratorObject<any>;
49+
50+
/**
51+
* Executes a provided function once for each element in the iterator.
52+
* @param callbackFn A function that is called for each element of the iterator
53+
* @returns A `Promise` that resolves when all elements have been processed
54+
*/
55+
forEach(callbackFn: (value: T, index: number) => void): Promise<void>;
56+
57+
/**
58+
* Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator.
59+
* @param mapper A function that transforms each element of the iterator
60+
* @returns A new `AsyncIterator`
61+
*/
62+
map(mapper: (value: T, index: number) => any): AsyncIteratorObject<T>;
63+
64+
/**
65+
* Reduces the elements of the iterator to a single value using the `reducer` function.
66+
* @param reducer A function that combines two elements of the iterator
67+
* @param initialValue An optional initial value to start the reduction
68+
* @returns A `Promise` that resolves to the reduced value
69+
*/
70+
reduce(reducer: (accumulator: any, value: T, index: number) => any, initialValue?: any): Promise<any>;
71+
72+
/**
73+
* Checks if any value in the iterator matches a given `predicate`
74+
* @param predicate A function that tests each element of the iterator
75+
* @returns A `Promise` that resolves to `true` if any element passes the `predicate`, otherwise `false`
76+
*/
77+
some(predicate: (value: T, index: number) => boolean): Promise<boolean>;
78+
79+
/**
80+
* Creates a new `AsyncIterator` that yields only the first `limit` elements from the original iterator.
81+
* @param limit The maximum number of elements to take
82+
* @returns A new `AsyncIterator`
83+
*/
84+
take(limit: number): AsyncIteratorObject<T>;
85+
86+
/**
87+
* Collects all elements from the iterator into an array.
88+
* @returns A `Promise` that resolves to an array containing all elements from the iterator
89+
*/
90+
toArray(): Promise<T[]>;
91+
}
92+
93+
interface Iterator<T> {
94+
/**
95+
* Creates an `AsyncIterator` from the current `Iterator`
96+
* @returns A new `AsyncIterator` instance
97+
*/
98+
toAsync(): AsyncIteratorObject<T>;
99+
}

packages/core-js-types/src/ts5-2/proposals/explicit-resource-management.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ declare var AsyncDisposableStack: AsyncDisposableStackConstructor;
193193

194194
interface IteratorObject<T, TReturn = any, TNext = any> extends Disposable {} // @type-options no-extends
195195

196-
interface AsyncIteratorObject<T, TReturn = any, TNext = any> extends AsyncDisposable {} // @type-options no-extends
196+
interface AsyncIteratorObject<T, TReturn = any, TNext = undefined> extends AsyncDisposable {} // @type-options no-extends
197197

198198
interface AsyncIteratorConstructor {}
199199

200200
declare var AsyncIterator: AsyncIteratorConstructor;
201201

202-
interface AsyncIterator<T, TReturn = any, TNext = any> {}
202+
interface AsyncIterator<T, TReturn = any, TNext = undefined> {}

scripts/build-entries/entries-definitions.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
$prototypeIterator,
77
$static,
88
$staticWithContext,
9+
$staticWithCustomType,
910
$patchableStatic,
1011
$namespace,
1112
$helper,
@@ -3087,9 +3088,10 @@ export const features = {
30873088
},
30883089
'url/parse': {
30893090
modules: ['web.url.parse'],
3090-
template: $static,
3091+
template: $staticWithCustomType,
30913092
namespace: 'URL',
30923093
name: 'parse',
3094+
customType: 'web/url-parse',
30933095
},
30943096
'url/to-json': {
30953097
modules: ['web.url.to-json'],

scripts/build-entries/templates.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ export const $staticWithContext = p => ({
201201
`,
202202
});
203203

204+
export const $staticWithCustomType = p => ({
205+
entry: dedent`
206+
${ importModules(p) }
207+
208+
var getBuiltInStaticMethod = ${ importInternal('get-built-in-static-method', p.level) }
209+
210+
module.exports = getBuiltInStaticMethod('${ p.namespace }', '${ p.name }');
211+
`,
212+
types: dedent`
213+
declare module '${ buildModulePath(p) }' {
214+
const method: ${ buildCoreJSTypeName(p.namespace, p.name) };
215+
export = method;
216+
}
217+
`,
218+
});
219+
204220
export const $patchableStatic = p => ({
205221
entry: dedent`
206222
${ importModules(p) }

tests/type-definitions/runner.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ const ALL_TESTS = process.env.ALL_TYPE_DEFINITIONS_TESTS === '1';
77

88
const targets = [
99
'esnext',
10-
'es2023',
10+
'es2022',
1111
'es6',
1212
];
1313
const typeScriptVersions = [
1414
'5.9',
1515
'5.8',
1616
'5.7',
1717
'5.6',
18-
// '5.5'
18+
// '5.5', fails with node types: Named property 'next' of types 'AsyncIterator<T, TReturn, TNext>' and 'AsyncIteratorObject<T, TReturn, TNext>' are not identical.
19+
// '5.4',
20+
// '5.3',
21+
// '5.2',
1922
];
2023
const envs = [
2124
null,
@@ -47,13 +50,13 @@ async function runTestsOnEnv({ typeScriptVersion, target, type, env, lib }) {
4750
$.verbose = false;
4851
const envLibName = env ? env.substring(0, env.lastIndexOf('@')) : '';
4952
const command = `npx -p typescript@${ typeScriptVersion }${ env ? ` -p ${ env }` : '' } `
50-
+ `tsc -p ${ type }/tsconfig.json --target ${ target } --lib ${ target }${ lib ? `,${ lib }` : '' }${ env ? ` --types @core-js/types${ type === 'pure' ? '/pure' : '/full' },${ envLibName }` : '' }`;
53+
+ `tsc -p ${ type }/tsconfig.json --target ${ target } --lib ${ target }${ lib ? `,${ lib }` : '' }${ env ? ` --types @core-js/types${ type === 'pure' ? '/pure' : '' },${ envLibName }` : '' }`;
5154
echo(`$ ${ command }`);
5255
try {
5356
if (env && lib) {
54-
await $({ cwd: getEnvPath(env) })`npx -p typescript@${ typeScriptVersion } tsc -p ./tsconfig.${ type }.json --target ${ target } --lib ${ target },${ lib } --types @core-js/types${ type === 'pure' ? '/pure' : '/full' },${ envLibName }`.quiet();
57+
await $({ cwd: getEnvPath(env) })`npx -p typescript@${ typeScriptVersion } tsc -p ./tsconfig.${ type }.json --target ${ target } --lib ${ target },${ lib } --types @core-js/types${ type === 'pure' ? '/pure' : '' },${ envLibName }`.quiet();
5558
} else if (env) {
56-
await $({ cwd: getEnvPath(env) })`npx -p typescript@${ typeScriptVersion } tsc -p ./tsconfig.${ type }.json --target ${ target } --lib ${ target } --types @core-js/types${ type === 'pure' ? '/pure' : '/full' },${ envLibName }`.quiet();
59+
await $({ cwd: getEnvPath(env) })`npx -p typescript@${ typeScriptVersion } tsc -p ./tsconfig.${ type }.json --target ${ target } --lib ${ target } --types @core-js/types${ type === 'pure' ? '/pure' : '' },${ envLibName }`.quiet();
5760
} else if (lib) {
5861
await $`npx -p typescript@${ typeScriptVersion } tsc -p ${ type }/tsconfig.json --target ${ target } --lib ${ target },${ lib }`.quiet();
5962
} else {

0 commit comments

Comments
 (0)