Skip to content

Commit

Permalink
feat!: improve type declarations for utils/map-arguments
Browse files Browse the repository at this point in the history
This change significantly improves the type definition of the mapArguments
function, transitioning from broad 'Function' types to specific, generic types
and implementing conditional this context based on thisArg presence.

BREAKING CHANGE: function signature and return type now more strictly typed
The mapArguments function now uses generic types instead of 'Function', and its
return type depends on thisArg presence. 

Users should review and update their usage of mapArguments, particularly:

-   Ensure provided functions match the new, stricter type requirements
-   Update any type assertions or checks where mapArguments is used
-   Pay special attention to contexts where this binding is significant

PR-URL: #2050
Closes: #1087

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Co-authored-by: Prajwal Kulkarni <prajwalkulkarni76@gmail.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
  • Loading branch information
prajwalkulkarni and Planeshifter authored Oct 14, 2024
1 parent e5b993a commit b74a08a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,57 @@
* var out = bar( 1, 2, 3 );
* // returns [ 2, 4, 6 ]
*/
declare function mapArguments( fcn: Function, clbk: Function, thisArg?: any ): Function;
declare function mapArguments<
T extends ( ...args: Array<any> ) => any,

Check warning on line 51 in lib/node_modules/@stdlib/utils/map-arguments/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 51 in lib/node_modules/@stdlib/utils/map-arguments/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type
C extends ( this: ThisParameterType<T>, value: Parameters<T>[number], index: number ) => any

Check warning on line 52 in lib/node_modules/@stdlib/utils/map-arguments/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type
>(
fcn: T,
clbk: C
): ( ...args: Parameters<T> ) => ReturnType<T>;

/**
* Returns a function that applies arguments to a provided function after transforming arguments according to a callback function.
*
* ## Notes
*
* - The callback function is provided the following arguments:
*
* - **value**: argument value.
* - **index**: argument index.
*
* @param fcn - input function
* @param clbk - callback function
* @param thisArg - input function context
* @returns function wrapper
*
* @example
* function foo( a, b, c ) {
* return [ a, b, c ];
* }
*
* function clbk( v ) {
* this.count += 1;
* return v * 2;
* }
*
* var thisArg = { 'count': 0 };
* var bar = mapArguments( foo, clbk, thisArg );
*
* var out = bar( 1, 2, 3 );
* // returns [ 2, 4, 6 ]
*
* var count = thisArg.count;
* // returns 3
*/
declare function mapArguments<
T extends ( ...args: Array<any> ) => any,

Check warning on line 93 in lib/node_modules/@stdlib/utils/map-arguments/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 93 in lib/node_modules/@stdlib/utils/map-arguments/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type
C extends ( this: ThisParameterType<T>, value: Parameters<T>[number], index: number ) => any,

Check warning on line 94 in lib/node_modules/@stdlib/utils/map-arguments/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type
ThisArg
>(
fcn: T,
clbk: C,
thisArg: ThisArg
): ( this: ThisArg, ...args: Parameters<T> ) => ReturnType<T>;


// EXPORTS //
Expand Down
32 changes: 23 additions & 9 deletions lib/node_modules/@stdlib/utils/map-arguments/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,37 @@

import mapArguments = require( './index' );

/**
* Callback function.
*
* @param v - argument
* @returns result
*/
function clbk( v: any ): any {
// FUNCTIONS //

function clbk<T>( v: T ): T {
return v;
}

function stringify( n: number ): string {
return n.toString();
}

function sum( ...numbers: Array<number> ): number {
return numbers.reduce( ( a, b ) => a + b, 0 );
}

function greet( this: { name: string }, greeting: string ): string {
return `${greeting}, ${this.name}!`; // eslint-disable-line no-invalid-this
}


// TESTS //

// The function returns a function...
{
mapArguments( ( x: any, y: any, z: any ): Array<any> => [ x, y, z ], clbk ); // $ExpectType Function
mapArguments( ( x: any, y: any, z: any ): Array<any> => [ x, y, z ], clbk, {} ); // $ExpectType Function
mapArguments( ( x: number, y: number, z: number ): Array<number> => [ x, y, z ], clbk ); // $ExpectType (x: number, y: number, z: number) => number[]
mapArguments( ( x: string, y: string, z: string ): Array<string> => [ x, y, z ], clbk, {} ); // $ExpectType (this: {}, x: string, y: string, z: string) => string[]
mapArguments( ( x: number, y: number ): number => x + y, clbk ); // $ExpectType (x: number, y: number) => number

mapArguments( stringify, ( x: number ) => x * 2 ); // $ExpectType (n: number) => string
mapArguments( sum, ( x: number ) => x * 2 ); // $ExpectType (...args: number[]) => number

mapArguments( greet, ( s: string ) => s.toUpperCase(), { 'name': 'World' } ); // $ExpectType (this: { name: string; }, greeting: string) => string
}

// The compiler throws an error if the function is provided a first argument other than a function...
Expand Down

1 comment on commit b74a08a

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
utils/map-arguments $\color{green}141/141$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}141/141$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.