Utilizing p-waterfall has led me to seek guidance on how to implement the TypeScript types it offers. You can find the relevant types here.
declare namespace pWaterfall {
type Task<ValueType, ReturnType> = (
previousValue: ValueType
) => ReturnType | PromiseLike<ReturnType>;
type InitialTask<ReturnType> = () => ReturnType | PromiseLike<ReturnType>;
}
declare const pWaterfall: {
<ReturnType>(tasks: [pWaterfall.InitialTask<ReturnType>]): Promise<ReturnType>;
<ValueType1, ReturnType>(
tasks: [
pWaterfall.InitialTask<ValueType1>,
pWaterfall.Task<ValueType1, ReturnType>
]
): Promise<ReturnType>;
<ValueType1, ValueType2, ReturnType>(
tasks: [
pWaterfall.InitialTask<ValueType1>,
pWaterfall.Task<ValueType1, ValueType2>,
pWaterfall.Task<ValueType2, ReturnType>
]
): Promise<ReturnType>;
...
An example has been created (see code below) which, when typed properly, will enhance my understanding of utilizing the provided TypeScript types with p-waterfall.
import pWaterfall from "p-waterfall";
interface User {
name: string;
}
const getItemsRequest = async (sliceCount: number): Promise<User[]> => {
return [{ name: "a" }, { name: "b" }, { name: "c" }].slice(0, sliceCount);
};
const getNames = (results: User[]): string[] => {
return results.map(item => item.name);
};
const countArrayLength = <T extends unknown[]>(results: T): number => {
return results.length;
};
(async () => {
const transformers = [getItemsRequest, getNames, countArrayLength];
const shouldBeTypeNumberButIsUnknown = await pWaterfall(transformers, 2);
console.log(`results`, shouldBeTypeNumberButIsUnknown); // 2
})();
shouldBeTypeNumberButIsUnknown
is currently labeled as unknown although it should ideally be of type number
, given that the last function passed into pWaterfall
returns a number.