Skip to content

Commit 6d74243

Browse files
committed
fix: ensure correct type when providing a dtype option
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 0ed631d commit 6d74243

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

lib/node_modules/@stdlib/ndarray/flatten/docs/types/index.d.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

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

23-
import { ndarray, Order, DataType } from '@stdlib/types/ndarray';
23+
import { ndarray, typedndarray, Order, DataTypeMap } from '@stdlib/types/ndarray';
2424

2525
/**
26-
* Interface defining function options.
26+
* Interface defining "base" function options.
2727
*/
28-
interface Options {
28+
interface BaseOptions {
2929
/**
3030
* Maximum number of dimensions to flatten.
3131
*
@@ -50,16 +50,47 @@ interface Options {
5050
* - Default: 'row-major'.
5151
*/
5252
order?: Order | 'same' | 'any';
53+
}
5354

55+
/**
56+
* Function options.
57+
*/
58+
type Options<U> = BaseOptions & {
5459
/**
5560
* Output ndarray data type.
56-
*
57-
* ## Notes
58-
*
59-
* - By default, the function returns an ndarray having the same data type as a provided input ndarray.
6061
*/
61-
dtype?: DataType;
62-
}
62+
dtype: U;
63+
};
64+
65+
/**
66+
* Returns a flattened copy of an input ndarray.
67+
*
68+
* ## Notes
69+
*
70+
* - The function **always** returns a copy of input ndarray data, even when an input ndarray already has the desired number of dimensions.
71+
* - By default, the function returns an ndarray having the same data type as a provided input ndarray.
72+
*
73+
* @param x - input ndarray
74+
* @param options - function options
75+
* @param options.depth - maximum number of dimensions to flatten
76+
* @param options.order - order in which input ndarray elements should be flattened
77+
* @param options.dtype - output ndarray data type
78+
* @returns output ndarray
79+
*
80+
* @example
81+
* var array = require( '@stdlib/ndarray/array' );
82+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
83+
*
84+
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
85+
* // return <ndarray>
86+
*
87+
* var y = flatten( x );
88+
* // returns <ndarray>
89+
*
90+
* var arr = ndarray2array( y );
91+
* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
92+
*/
93+
declare function flatten<T extends ndarray>( x: T, options?: BaseOptions ): T;
6394

6495
/**
6596
* Returns a flattened copy of an input ndarray.
@@ -72,6 +103,7 @@ interface Options {
72103
* @param options - function options
73104
* @param options.depth - maximum number of dimensions to flatten
74105
* @param options.order - order in which input ndarray elements should be flattened
106+
* @param options.dtype - output ndarray data type
75107
* @returns output ndarray
76108
*
77109
* @example
@@ -87,7 +119,7 @@ interface Options {
87119
* var arr = ndarray2array( y );
88120
* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
89121
*/
90-
declare function flatten<T extends ndarray>( x: T, options?: Options ): T;
122+
declare function flatten<T = unknown, U extends keyof DataTypeMap<T> = 'generic'>( x: typedndarray<T>, options: Options<U> ): DataTypeMap<T>[U];
91123

92124

93125
// EXPORTS //

lib/node_modules/@stdlib/ndarray/flatten/docs/types/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import flatten = require( './index' );
3131
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), {} ); // $ExpectType float64ndarray
3232
flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), {} ); // $ExpectType complex128ndarray
3333
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), {} ); // $ExpectType genericndarray<number>
34+
35+
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': 'float32' } ); // $ExpectType float32ndarray
36+
flatten( zeros( 'int32', [ 2, 2, 2 ], 'row-major' ), { 'dtype': 'float64' } ); // $ExpectType float64ndarray
37+
flatten( zeros( 'int32', [ 2, 2, 2 ], 'row-major' ), { 'dtype': 'generic' } ); // $ExpectType genericndarray<number>
3438
}
3539

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

0 commit comments

Comments
 (0)