Skip to content

Commit e5226a5

Browse files
committed
Types refactoring
1 parent 953c6d6 commit e5226a5

28 files changed

+298
-259
lines changed

packages/core-js-types/src/base/proposals/array-find-from-last.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ interface Float64Array { // @type-options: no-export
267267
findLast<S extends number>(predicate: (value: number, index: number, array: this) => value is S, thisArg?: any): S | undefined;
268268
findLast(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): number | undefined;
269269

270+
/**
271+
* Returns the index of the last element in the array where predicate is true, and -1
272+
* otherwise.
273+
* @param predicate - findLastIndex calls predicate once for each element of the array, in descending
274+
* order, until it finds one where predicate returns true. If such an element is found,
275+
* findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
276+
* @param thisArg - If provided, it will be used as this value for each invocation of
277+
* predicate. If it is not provided, undefined is used instead.
278+
*/
270279
findLastIndex(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): number;
271280
}
272281

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="../core-js-types/async-iterator" />
2+
3+
// Motivation: Custom type needed to keep generics strict
4+
5+
// https://github.com/tc39/proposal-async-iterator-helpers
6+
7+
declare namespace CoreJS {
8+
export type AsyncIteratorFlatMap<T, U> = (callback: (value: T, index: number) => Iterator<U, unknown, undefined> | Iterable<U, unknown, undefined> | AsyncIterator<U> | AsyncIterable<U>) => AsyncIteratorObject<U, undefined, unknown>;
9+
10+
export type AsyncIteratorMap<T, U> = (callback: (value: T, index: number) => U) => AsyncIteratorObject<U, undefined, unknown>;
11+
12+
export type AsyncIteratorReduce<T, U = T> = (callback: (accumulator: U, value: T, index: number) => U, initialValue?: U) => Promise<U>;
13+
}

packages/core-js-types/src/base/proposals/async-iterator-helpers.d.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@ interface AsyncIterator<T, TReturn = any, TNext = any> {
2626
* @param predicate - A function that tests each element of the iterator
2727
* @returns A promise that resolves to `true` if all elements pass the test, otherwise `false`
2828
*/
29-
every(predicate: (value: T, index: number) => boolean): Promise<boolean>;
29+
every(predicate: (value: T, index: number) => unknown): Promise<boolean>;
3030

3131
/**
3232
* Creates a new `AsyncIterator` that contains only the elements that pass the `predicate` function.
3333
* @param predicate - A function that tests each element of the iterator
3434
* @returns A new `AsyncIterator`
3535
*/
36+
filter<S extends T>(predicate: (value: T, index: number) => value is S): AsyncIteratorObject<S, undefined, unknown>;
3637
filter(predicate: (value: T, index: number) => boolean): AsyncIteratorObject<T>;
3738

3839
/**
3940
* Finds the first element in the iterator that satisfies the `predicate` function.
4041
* @param predicate - A function that tests each element of the iterator
4142
* @returns A `Promise` that resolves to the found element, or `undefined` if no element satisfies the `predicate`
4243
*/
43-
find(predicate: (value: T, index: number) => boolean): Promise<T>;
44+
find<S extends T>(predicate: (value: T, index: number) => value is S): Promise<S | undefined>;
45+
find(predicate: (value: T, index: number) => boolean): Promise<T | undefined>;
4446

4547
/**
4648
* Creates a new `AsyncIterator` by applying the `mapper` function to each element of the original iterator and flattening the result.
47-
* @param mapper - A function that transforms each element of the iterator
49+
* @param callback - A function that transforms each element of the iterator
4850
* @returns A new `AsyncIterator`
4951
*/
50-
flatMap(mapper: (value: T, index: number) => any): AsyncIteratorObject<any>;
52+
flatMap<U>(callback: (value: T, index: number) => Iterator<U, unknown, undefined> | Iterable<U, unknown, undefined> | AsyncIterator<U> | AsyncIterable<U>): AsyncIteratorObject<U, undefined, unknown>;
5153

5254
/**
5355
* Executes a provided function once for each element in the iterator.
@@ -61,22 +63,23 @@ interface AsyncIterator<T, TReturn = any, TNext = any> {
6163
* @param mapper - A function that transforms each element of the iterator
6264
* @returns A new `AsyncIterator`
6365
*/
64-
map(mapper: (value: T, index: number) => any): AsyncIteratorObject<T>;
66+
map<U>(mapper: (value: T, index: number) => U): AsyncIteratorObject<U, undefined, unknown>;
6567

6668
/**
6769
* Reduces the elements of the iterator to a single value using the `reducer` function.
6870
* @param reducer - A function that combines two elements of the iterator
6971
* @param initialValue - An optional initial value to start the reduction
7072
* @returns A `Promise` that resolves to the reduced value
7173
*/
72-
reduce(reducer: (accumulator: any, value: T, index: number) => any, initialValue?: any): Promise<any>;
74+
reduce<U>(reducer: (accumulator: U, value: T, index: number) => U, initialValue?: U): Promise<U>;
75+
reduce(reducer: (accumulator: T, value: T, index: number) => T, initialValue?: T): Promise<T>;
7376

7477
/**
7578
* Checks if any value in the iterator matches a given `predicate`
7679
* @param predicate - A function that tests each element of the iterator
7780
* @returns A `Promise` that resolves to `true` if any element passes the `predicate`, otherwise `false`
7881
*/
79-
some(predicate: (value: T, index: number) => boolean): Promise<boolean>;
82+
some(predicate: (value: T, index: number) => unknown): Promise<boolean>;
8083

8184
/**
8285
* Creates a new `AsyncIterator` that yields only the first `limit` elements from the original iterator.

packages/core-js-types/src/base/proposals/await-dictionary.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface PromiseConstructor {
99
* @param promises - An object of promises
1010
* @returns A new `Promise` that resolves to an object of fulfilled values or rejects if any promise rejects.
1111
*/
12-
allKeyed<D extends Record<PropertyKey, Promise<any>>>(promises: D): Promise <{ [k in keyof D]: Awaited<D[k]> }>;
12+
allKeyed<D extends Record<PropertyKey, Promise<any>>>(promises: D): Promise<{ [k in keyof D]: Awaited<D[k]> }>;
1313

1414
/**
1515
* Takes an object whose values are promises and returns a single `Promise` that resolves
@@ -18,7 +18,7 @@ interface PromiseConstructor {
1818
* @returns A new Promise that resolves to an object with the same keys as the input object,
1919
* where each key maps to the settlement result (`{ status, value }` or `{ status, reason }`) of the corresponding promise.
2020
*/
21-
allSettledKeyed<D extends Record<PropertyKey, Promise<any>>>(promises: D): Promise <{ [k in keyof D]: CoreJS.CoreJSPromiseSettledResult<Awaited<D[k]>> }>;
21+
allSettledKeyed<D extends Record<PropertyKey, Promise<any>>>(promises: D): Promise<{ [k in keyof D]: CoreJS.CoreJSPromiseSettledResult<Awaited<D[k]>> }>;
2222
}
2323

2424
declare var Promise: PromiseConstructor;

packages/core-js-types/src/base/proposals/change-array-by-copy.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ interface Uint32Array { // @type-options: no-export
274274
* a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
275275
* value otherwise. If omitted, the elements are sorted in ascending order.
276276
* ```ts
277-
* const myNums = Int32Array.from([11, 2, -22, 1]);
278-
* myNums.toSorted((a, b) => a - b) // Int32Array(4) [-22, 1, 2, 11]
277+
* const myNums = Uint32Array.from([11, 2, -22, 1]);
278+
* myNums.toSorted((a, b) => a - b) // Uint32Array(4) [-22, 1, 2, 11]
279279
* ```
280280
*/
281281
toSorted(compareFn?: (a: number, b: number) => number): Uint32Array;
@@ -303,7 +303,7 @@ interface Float32Array { // @type-options: no-export
303303
* value otherwise. If omitted, the elements are sorted in ascending order.
304304
* ```ts
305305
* const myNums = Float32Array.from([11.25, 2, -22.5, 1]);
306-
* myNums.toSorted((a, b) => a - b) // Float32Array(4) [-22.5, 1, 2, 11.5]
306+
* myNums.toSorted((a, b) => a - b) // Float32Array(4) [-22.5, 1, 2, 11.25]
307307
* ```
308308
*/
309309
toSorted(compareFn?: (a: number, b: number) => number): Float32Array;
@@ -331,7 +331,7 @@ interface Float64Array { // @type-options: no-export
331331
* value otherwise. If omitted, the elements are sorted in ascending order.
332332
* ```ts
333333
* const myNums = Float64Array.from([11.25, 2, -22.5, 1]);
334-
* myNums.toSorted((a, b) => a - b) // Float64Array(4) [-22.5, 1, 2, 11.5]
334+
* myNums.toSorted((a, b) => a - b) // Float64Array(4) [-22.5, 1, 2, 11.25]
335335
* ```
336336
*/
337337
toSorted(compareFn?: (a: number, b: number) => number): Float64Array;

packages/core-js-types/src/base/proposals/collection-of-from.d.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ declare var Map: MapConstructor;
2424

2525
interface SetConstructor {
2626
/**
27-
* Creates a new `Set` instance from an iterable or array-like object of [key, value] pairs.
28-
* Optionally, applies a mapping function to each pair.
29-
* @param source - Iterable or array-like object of [key, value] pairs.
30-
* @param mapFn - Function to call on every [key, value] pair before adding to the `Set`.
31-
* @param thisArg - Value to use as this when executing mapFn.
32-
* @returns A new `Set` instance.
27+
* Creates a new `Set` instance from an iterable, optionally transforming elements with a mapping function.
28+
* @param source - Iterable whose elements will be added to the new Set.
29+
* @param mapFn - Optional mapping function. Transforms each element before adding to Set.
30+
* @param thisArg - Value to use as `this` when executing `mapFn`.
31+
* @returns A new `Set` instance
3332
*/
3433
from<T, U = T>(source: Iterable<T>, mapFn?: (value: T, index: number) => U, thisArg?: any): Set<U>;
3534

3635
/**
37-
* Creates a new `Set` instance from a variable number of arguments,
38-
* where each pair of arguments is interpreted as a key and a value.
39-
* @param items - An even number of arguments representing key-value pairs.
40-
* @returns A new `Set` instance.
36+
* Creates a new `Set` instance from a variable number of arguments.
37+
* Each argument becomes an element of the Set.
38+
* @param items - Zero or more arguments to add as Set elements.
39+
* @returns A new `Set` instance
4140
*/
4241
of<T>(...items: T[]): Set<T>;
4342
}
@@ -59,7 +58,7 @@ interface WeakMapConstructor {
5958
* Creates a new `WeakMap` instance from a variable number of arguments,
6059
* where each pair of arguments is interpreted as a key and a value.
6160
* @param items - An even number of arguments representing key-value pairs.
62-
* @returns A new `Weak` instance.
61+
* @returns A new `WeakMap` instance.
6362
*/
6463
of<K extends object, V>(...items: [K, V][]): WeakMap<K, V>;
6564
}
@@ -68,20 +67,19 @@ declare var WeakMap: WeakMapConstructor;
6867

6968
interface WeakSetConstructor {
7069
/**
71-
* Creates a new `WeakSet` instance from an iterable or array-like object of [key, value] pairs.
72-
* Optionally, applies a mapping function to each pair.
73-
* @param source - Iterable or array-like object of [key, value] pairs.
74-
* @param mapFn - Function to call on every [key, value] pair before adding to the `WeakSet`.
75-
* @param thisArg - Value to use as this when executing mapFn.
76-
* @returns A new `WeakSet` instance.
70+
* Creates a new `WeakSet` instance from an iterable of objects, optionally transforming with mapFn.
71+
*
72+
* @param source - Iterable of objects to add to WeakSet.
73+
* @param mapFn - Optional mapping function transforming each object.
74+
* @param thisArg - Value to use as `this` in `mapFn`.
75+
* @returns New `WeakSet` instance.
7776
*/
7877
from<T extends object, U extends object = T>(source: Iterable<T>, mapFn?: (value: T, index: number) => U, thisArg?: any): WeakSet<U>;
7978

8079
/**
81-
* Creates a new `WeakSet` instance from a variable number of arguments,
82-
* where each pair of arguments is interpreted as a key and a value.
83-
* @param items - An even number of arguments representing key-value pairs.
84-
* @returns A new `WeakSet` instance.
80+
* Creates a new `WeakSet` instance from object arguments.
81+
* @param items - Zero or more objects to add as WeakSet elements.
82+
* @returns New `WeakSet` instance.
8583
*/
8684
of<T extends object>(...items: T[]): WeakSet<T>;
8785
}

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -148,33 +148,7 @@ interface AsyncDisposableStack {
148148
defer(onDisposeAsync: () => PromiseLike<void> | void): void;
149149

150150
/**
151-
* Move all resources out of this stack and into a new `DisposableStack`, and marks this stack as disposed.
152-
* @example
153-
* ```ts
154-
* class C {
155-
* #res1: Disposable;
156-
* #res2: Disposable;
157-
* #disposables: DisposableStack;
158-
* constructor() {
159-
* // stack will be disposed when exiting constructor for any reason
160-
* using stack = new DisposableStack();
161-
*
162-
* // get first resource
163-
* this.#res1 = stack.use(getResource1());
164-
*
165-
* // get second resource. If this fails, both `stack` and `#res1` will be disposed.
166-
* this.#res2 = stack.use(getResource2());
167-
*
168-
* // all operations succeeded, move resources out of `stack` so that they aren't disposed
169-
* // when constructor exits
170-
* this.#disposables = stack.move();
171-
* }
172-
*
173-
* [Symbol.dispose]() {
174-
* this.#disposables.dispose();
175-
* }
176-
* }
177-
* ```
151+
* Move all resources out of this stack and into a new `AsyncDisposableStack`, and marks this stack as disposed.
178152
*/
179153
move(): AsyncDisposableStack;
180154

0 commit comments

Comments
 (0)