Skip to content

refactor: improve type definitions for ndarray array #1392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/node_modules/@stdlib/ndarray/array/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface Options {
/**
* Specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']).
*/
submode?: Array<string>;
submode?: Array<Mode>;

/**
* Boolean indicating whether to copy source data to a new data buffer (default: false).
Expand Down Expand Up @@ -76,7 +76,7 @@ interface Options {
/**
* Interface describing function options.
*/
interface OptionsWithShape extends Options {
interface OptionsWithShape<T> extends Options {
/**
* Array shape.
*/
Expand All @@ -89,13 +89,13 @@ interface OptionsWithShape extends Options {
*
* - If provided along with a `buffer` argument, the argument takes precedence.
*/
buffer?: ArrayLike<any>;
buffer?: ArrayLike<T>;
}

/**
* Interface describing function options.
*/
interface OptionsWithBuffer extends Options {
interface OptionsWithBuffer<T> extends Options {
/**
* Array shape.
*/
Expand All @@ -108,13 +108,13 @@ interface OptionsWithBuffer extends Options {
*
* - If provided along with a `buffer` argument, the argument takes precedence.
*/
buffer: ArrayLike<any>;
buffer: ArrayLike<T>;
}

/**
* Interface describing function options.
*/
interface ExtendedOptions extends Options {
interface ExtendedOptions<T> extends Options {
/**
* Array shape.
*/
Expand All @@ -127,7 +127,7 @@ interface ExtendedOptions extends Options {
*
* - If provided along with a `buffer` argument, the argument takes precedence.
*/
buffer?: ArrayLike<any>;
buffer?: ArrayLike<T>;
}

/**
Expand Down Expand Up @@ -164,7 +164,11 @@ interface ExtendedOptions extends Options {
* var v = arr.get( 0 );
* // returns [ 1, 2 ]
*/
declare function array<T = unknown>( options: OptionsWithShape | OptionsWithBuffer ): typedndarray<T>;

type OptionsType<T> = OptionsWithShape<T> | OptionsWithBuffer<T>;
declare function array<T = unknown>( options: OptionsWithShape<T> | OptionsWithBuffer<T> ): typedndarray<undefined extends T
? ( OptionsType<T> extends { dtype: infer D } ? D : never )
: ( OptionsType<T> extends { dtype: infer D } ? D : T )>;

/**
* Returns a multidimensional array.
Expand Down Expand Up @@ -220,7 +224,7 @@ declare function array<T = unknown>( options: OptionsWithShape | OptionsWithBuff
* var v = arr.get( 0, 0 );
* // returns 1.0
*/
declare function array<T = unknown>( buffer: ArrayLike<any>, options?: ExtendedOptions ): typedndarray<T>;
declare function array<T = unknown>( buffer: ArrayLike<T>, options?: ExtendedOptions<T> ): typedndarray<T extends undefined ? ( typeof options extends { dtype: infer D } ? D : never ) : T>;


// EXPORTS //
Expand Down
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/ndarray/array/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import array = require( './index' );

// The function returns an ndarray...
{
array<number>( [ [ 1, 2 ], [ 3, 4 ] ] ); // $ExpectType typedndarray<number>
array<Array<number>>( [ [ 1, 2 ], [ 3, 4 ] ] ); // $ExpectType typedndarray<number[]>
array<number>( new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), { 'shape': [ 2, 2 ] } ); // $ExpectType typedndarray<number>
array<number>( { 'shape': [ 2, 2 ] } ); // $ExpectType typedndarray<number>
array<number>( { 'buffer': [ [ 1, 2 ], [ 3, 4 ] ] } ); // $ExpectType typedndarray<number>
array<Array<number>>( { 'buffer': [ [ 1, 2 ], [ 3, 4 ] ] } ); // $ExpectType typedndarray<number[]>
}

// The compiler throws an error if the function is provided a first argument which is not an array, buffer, or options object...
Expand Down