Skip to content

Commit

Permalink
feat!: refactor declarations to use generics
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 27, 2023
1 parent ee49f1e commit 2abd8cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,54 @@
* limitations under the License.
*/

// TypeScript Version: 2.0
// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/object';

/**
* Returns the n-fold Cartesian product.
*
* ## Notes
*
* - If provided one or more empty arrays, the function returns an empty array.
*
* @param x1 - first input array
* @param x2 - second input array
* @returns Cartesian product
*
* @example
* var x1 = [ 1, 2, 3 ];
* var x2 = [ 4, 5 ];
*
* var out = nCartesianProduct( x1, x2 );
* // returns [ [ 1, 4 ], [ 1, 5 ], [ 2, 4 ], [ 2, 5 ], [ 3, 4 ], [ 3, 5 ] ]
*/
declare function nCartesianProduct<T = any, U = any>( x1: Collection<T>, x2: Collection<U> ): Array<[T, U]>; // tslint:disable-line:max-line-length

/**
* Returns the n-fold Cartesian product.
*
* ## Notes
*
* - If provided one or more empty arrays, the function returns an empty array.
*
* @param x1 - first input array
* @param x2 - second input array
* @param x3 - third input array
* @returns Cartesian product
*
* @example
* var x1 = [ 1, 2, 3 ];
* var x2 = [ 4, 5 ];
* var x3 = [ 6 ];
*
* var out = nCartesianProduct( x1, x2, x3 );
* // returns [ [ 1, 4, 6 ], [ 1, 5, 6 ], [ 2, 4, 6 ], [ 2, 5, 6 ], [ 3, 4, 6 ], [ 3, 5, 6 ] ]
*/
declare function nCartesianProduct<T = any, U = any, V = any>( x1: Collection<T>, x2: Collection<U>, x3: Collection<V> ): Array<[T, U, V]>; // tslint:disable-line:max-line-length

/**
* Returns the n-fold Cartesian product.
*
Expand All @@ -41,7 +83,7 @@ import { Collection } from '@stdlib/types/object';
* var out = nCartesianProduct( x1, x2 );
* // returns [ [ 1, 4 ], [ 1, 5 ], [ 2, 4 ], [ 2, 5 ], [ 3, 4 ], [ 3, 5 ] ]
*/
declare function nCartesianProduct( x1: Collection, x2: Collection, ...xN: Array<Collection> ): Array<Array<any>>; // tslint:disable-line:max-line-length
declare function nCartesianProduct<T = any, U = any, V = any>( x1: Collection<T>, x2: Collection<U>, ...xN: Array<Collection<V>> ): Array<Array<T | U | V>>; // tslint:disable-line:max-line-length


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import nCartesianProduct = require( './index' );

// The function returns an array of arrays...
{
nCartesianProduct( [ 1, 2, 3, 4 ], [ 1, 3 ] ); // $ExpectType any[][]
nCartesianProduct( [ 1, 2, 3, 4 ], [ 1, 3 ] ); // $ExpectType [number, number][]
nCartesianProduct( [ 1, 2, 3, 4 ], [ 1, 3 ], [ 6 ] ); // $ExpectType [number, number, number][]
nCartesianProduct( [ 1, 2, 3, 4 ], [ 1, 3 ], [ 6 ], [ 7 ] ); // $ExpectType number[][]
}

// The compiler throws an error if the function is provided a first argument which is not an array-like object...
Expand Down

0 comments on commit 2abd8cd

Please sign in to comment.