Skip to content

Commit 2095d59

Browse files
authored
🤖 Merge PR DefinitelyTyped#73517 Fix type definition of cross() for d3-array v2 and v3 by @szenadam
1 parent 29420d4 commit 2095d59

File tree

4 files changed

+100
-38
lines changed

4 files changed

+100
-38
lines changed

‎types/d3-array/d3-array-tests.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,9 +2530,9 @@ const chars = ["x", "y"];
25302530
const nums = [1, 2];
25312531

25322532
crossed = d3Array.cross(chars, nums);
2533-
crossed = d3Array.cross<string, number>(chars, nums);
2533+
crossed = d3Array.cross<[string, number]>(chars, nums);
25342534

2535-
let strArray: string[] = d3Array.cross<number, number, string>([2, 3], [5, 6], (a, b) => (a + b) + "px");
2535+
let strArray: string[] = d3Array.cross<[number, number], string>([2, 3], [5, 6], (a, b) => (a + b) + "px");
25362536
strArray = d3Array.cross([2, 3], [5, 6], (a, b) => {
25372537
const aa: number = a;
25382538
const bb: number = b;
@@ -2543,9 +2543,9 @@ const readonlyChars = chars as readonly string[];
25432543
const readonlyNums = new Uint8Array(nums);
25442544

25452545
crossed = d3Array.cross(readonlyChars, readonlyNums);
2546-
crossed = d3Array.cross<string, number>(readonlyChars, readonlyNums);
2546+
crossed = d3Array.cross<[string, number]>(readonlyChars, readonlyNums);
25472547

2548-
strArray = d3Array.cross<number, number, string>(
2548+
strArray = d3Array.cross<[number, number], string>(
25492549
[2, 3] as readonly number[],
25502550
new Uint8ClampedArray([5, 6]),
25512551
(a, b) => (a + b) + "px",

‎types/d3-array/index.d.ts‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -648,25 +648,56 @@ export function count<TObject>(
648648
): number;
649649

650650
/**
651-
* Returns the Cartesian product of the two arrays a and b.
652-
* For each element i in the specified array a and each element j in the specified array b, in order,
653-
* it creates a two-element array for each pair.
651+
* Computes the Cartesian product of any number of iterables.
654652
*
655-
* @param a First input array.
656-
* @param b Second input array.
657-
*/
658-
export function cross<S, T>(a: Iterable<S>, b: Iterable<T>): Array<[S, T]>;
653+
* When called **without** a reducer, the result is an array of tuples,
654+
* where each tuple contains one element from each input iterable.
655+
*
656+
* @typeParam T - A tuple type describing the element type of each iterable argument.
657+
* For example, passing `[number[], string[]]` infers `T` as `[number, string]`.
658+
*
659+
* @param iterables - Two or more iterables to combine into a Cartesian product.
660+
* @returns An array of tuples containing one value from each iterable.
661+
*
662+
* @example
663+
* ```ts
664+
* const nums = [1, 2];
665+
* const chars = ['a', 'b'];
666+
* const out = cross(nums, chars);
667+
* // ^? type: [number, string][]
668+
* // Example value: [[1,'a'], [1,'b'], [2,'a'], [2,'b']]
669+
* ```
670+
*/
671+
export function cross<T extends unknown[]>(
672+
...iterables: { [K in keyof T]: Iterable<T[K]> }
673+
): T[];
659674

660675
/**
661-
* Returns the Cartesian product of the two arrays a and b.
662-
* For each element i in the specified array a and each element j in the specified array b, in order,
663-
* invokes the specified reducer function passing the element i and element j.
676+
* Computes the Cartesian product of any number of iterables and then applies
677+
* a reducer to each tuple, returning the reduced values.
664678
*
665-
* @param a First input array.
666-
* @param b Second input array.
667-
* @param reducer A reducer function taking as input an element from "a" and "b" and returning a reduced value.
668-
*/
669-
export function cross<S, T, U>(a: Iterable<S>, b: Iterable<T>, reducer: (a: S, b: T) => U): U[];
679+
* The final argument **must** be the reducer function; all prior arguments are iterables.
680+
*
681+
* @typeParam T - A tuple type describing the element type of each iterable argument.
682+
* @typeParam U - The result type produced by the reducer.
683+
*
684+
* @param args - The iterables to combine, followed by a reducer function.
685+
* @param args.reducer - A function invoked with one element from each iterable
686+
* (spread as individual parameters) that returns a reduced value.
687+
* @returns An array of reduced values returned by the reducer.
688+
*
689+
* @example
690+
* ```ts
691+
* const nums = [1, 2];
692+
* const chars = ['a', 'b'];
693+
* const out = cross(nums, chars, (n, c) => `${n}${c}`);
694+
* // ^? type: string[]
695+
* // Example value: ['1a', '1b', '2a', '2b']
696+
* ```
697+
*/
698+
export function cross<T extends unknown[], U>(
699+
...args: [...iterables: { [K in keyof T]: Iterable<T[K]> }, reducer: (...values: T) => U]
700+
): U[];
670701

671702
/**
672703
* Merges the specified arrays into a single array.

‎types/d3-array/v2/d3-array-tests.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,9 +901,9 @@ const chars = ["x", "y"];
901901
const nums = [1, 2];
902902

903903
crossed = d3Array.cross(chars, nums);
904-
crossed = d3Array.cross<string, number>(chars, nums);
904+
crossed = d3Array.cross<[string, number]>(chars, nums);
905905

906-
let strArray: string[] = d3Array.cross<number, number, string>([2, 3], [5, 6], (a, b) => (a + b) + "px");
906+
let strArray: string[] = d3Array.cross<[number, number], string>([2, 3], [5, 6], (a, b) => (a + b) + "px");
907907
strArray = d3Array.cross([2, 3], [5, 6], (a, b) => {
908908
const aa: number = a;
909909
const bb: number = b;
@@ -914,9 +914,9 @@ const readonlyChars = chars as readonly string[];
914914
const readonlyNums = new Uint8Array(nums);
915915

916916
crossed = d3Array.cross(readonlyChars, readonlyNums);
917-
crossed = d3Array.cross<string, number>(readonlyChars, readonlyNums);
917+
crossed = d3Array.cross<[string, number]>(readonlyChars, readonlyNums);
918918

919-
strArray = d3Array.cross<number, number, string>(
919+
strArray = d3Array.cross<[number, number], string>(
920920
[2, 3] as readonly number[],
921921
new Uint8ClampedArray([5, 6]),
922922
(a, b) => (a + b) + "px",

‎types/d3-array/v2/index.d.ts‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -702,25 +702,56 @@ export function count<TObject>(
702702
): number;
703703

704704
/**
705-
* Returns the Cartesian product of the two arrays a and b.
706-
* For each element i in the specified array a and each element j in the specified array b, in order,
707-
* it creates a two-element array for each pair.
705+
* Computes the Cartesian product of any number of iterables.
708706
*
709-
* @param a First input array.
710-
* @param b Second input array.
711-
*/
712-
export function cross<S, T>(a: Iterable<S>, b: Iterable<T>): Array<[S, T]>;
707+
* When called **without** a reducer, the result is an array of tuples,
708+
* where each tuple contains one element from each input iterable.
709+
*
710+
* @typeParam T - A tuple type describing the element type of each iterable argument.
711+
* For example, passing `[number[], string[]]` infers `T` as `[number, string]`.
712+
*
713+
* @param iterables - Two or more iterables to combine into a Cartesian product.
714+
* @returns An array of tuples containing one value from each iterable.
715+
*
716+
* @example
717+
* ```ts
718+
* const nums = [1, 2];
719+
* const chars = ['a', 'b'];
720+
* const out = cross(nums, chars);
721+
* // ^? type: [number, string][]
722+
* // Example value: [[1,'a'], [1,'b'], [2,'a'], [2,'b']]
723+
* ```
724+
*/
725+
export function cross<T extends unknown[]>(
726+
...iterables: { [K in keyof T]: Iterable<T[K]> }
727+
): T[];
713728

714729
/**
715-
* Returns the Cartesian product of the two arrays a and b.
716-
* For each element i in the specified array a and each element j in the specified array b, in order,
717-
* invokes the specified reducer function passing the element i and element j.
730+
* Computes the Cartesian product of any number of iterables and then applies
731+
* a reducer to each tuple, returning the reduced values.
718732
*
719-
* @param a First input array.
720-
* @param b Second input array.
721-
* @param reducer A reducer function taking as input an element from "a" and "b" and returning a reduced value.
722-
*/
723-
export function cross<S, T, U>(a: Iterable<S>, b: Iterable<T>, reducer: (a: S, b: T) => U): U[];
733+
* The final argument **must** be the reducer function; all prior arguments are iterables.
734+
*
735+
* @typeParam T - A tuple type describing the element type of each iterable argument.
736+
* @typeParam U - The result type produced by the reducer.
737+
*
738+
* @param args - The iterables to combine, followed by a reducer function.
739+
* @param args.reducer - A function invoked with one element from each iterable
740+
* (spread as individual parameters) that returns a reduced value.
741+
* @returns An array of reduced values returned by the reducer.
742+
*
743+
* @example
744+
* ```ts
745+
* const nums = [1, 2];
746+
* const chars = ['a', 'b'];
747+
* const out = cross(nums, chars, (n, c) => `${n}${c}`);
748+
* // ^? type: string[]
749+
* // Example value: ['1a', '1b', '2a', '2b']
750+
* ```
751+
*/
752+
export function cross<T extends unknown[], U>(
753+
...args: [...iterables: { [K in keyof T]: Iterable<T[K]> }, reducer: (...values: T) => U]
754+
): U[];
724755

725756
/**
726757
* Merges the specified arrays into a single array.

0 commit comments

Comments
 (0)