Skip to content

refactor: improve type definitions for async/if else #1381

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

Merged
merged 3 commits into from
Feb 25, 2024
Merged
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
13 changes: 11 additions & 2 deletions lib/node_modules/@stdlib/utils/async/if-else/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @param error - encountered error or null
* @param result - `x` or `y`
*/
type Callback = ( error: Error | null, result: any ) => void;
type Callback<T> = ( error: Error | null, result: T ) => void;

/**
* Predicate callback function.
Expand Down Expand Up @@ -61,6 +61,15 @@ type PredicateCallback = PredicateNullary | PredicateUnary | PredicateBinary;
*/
type Predicate = ( clbk: PredicateCallback ) => void;

/**
* Determines the relationship between two types `T` and `U`, and returns:
*
* - `T` if `T` is assignable to `U`,
* - `U` if `U` is assignable to `T`,
* - A union of `U | T` as a fallback.
*/
type ResultFunction<T, U> = T extends U ? T : U extends T ? U : U | T;

/**
* If a predicate function returns a truthy value, returns `x`; otherwise, returns `y`.
*
Expand All @@ -87,7 +96,7 @@ type Predicate = ( clbk: PredicateCallback ) => void;
* }
* ifelseAsync( predicate, 1.0, -1.0, done );
*/
declare function ifelseAsync( predicate: Predicate, x: any, y: any, done: Callback ): void;
declare function ifelseAsync<T, U = T>( predicate: Predicate, x: T, y: U, done: Callback<ResultFunction<T, U>> ): void;


// EXPORTS //
Expand Down