Skip to content

Commit

Permalink
fix flow typings and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed May 2, 2024
1 parent 1972559 commit ddbfb05
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions test/flow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expectError, expectType } from 'tsd';

import { flow } from '../es';

const strToNum = (str: string) => Number(str);
const numToStr = (num: number) => String(num);

expectType<number>(flow(1, []));
expectType<string>(flow(1, [numToStr]));
expectType<number>(flow(1, [numToStr, strToNum]));
expectType<string>(flow(1, [numToStr, strToNum, numToStr]));
expectType<number>(flow(1, [numToStr, strToNum, numToStr, strToNum]));
expectType<string>(flow(1, [numToStr, strToNum, numToStr, strToNum, numToStr]));
expectType<number>(flow(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum]));
expectType<string>(flow(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr]));
expectType<number>(flow(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum]));
expectType<string>(flow(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr]));

// with 10+, if you don't set the generic it returns unknown
expectType<unknown>(flow(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum]));
// setting the final return type has no typechecking against the actual chain of functions
expectType<Date>(flow<number, Date>(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum]));
// the Seed type has to match though, or it will error
expectError(flow<string, Date>(1, [numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum, numToStr, strToNum]));

// same applies if you have generic array of functions, even if the arg and return are typed
const seriesOfFunctions: Array<(a: string) => string> = [];
expectType<unknown>(flow(1, seriesOfFunctions));
// setting the final return type has no typechecking against the actual chain of functions
expectType<Date>(flow<number, Date>(1, seriesOfFunctions));
// the Seed type has to match though, or it will error
expectError(flow<string, Date>(1, seriesOfFunctions));
2 changes: 1 addition & 1 deletion types/flow.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export function flow<S, R1, R2, R3, R4, R5, R6, R7>(seed: S, pipeline: [(a: S) =
export function flow<S, R1, R2, R3, R4, R5, R6, R7, R8>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5, (a: R5) => R6, (a: R6) => R7, (a: R7) => R8]): R8;
export function flow<S, R1, R2, R3, R4, R5, R6, R7, R8, R9>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5, (a: R5) => R6, (a: R6) => R7, (a: R7) => R8, (a: R8) => R9]): R9;
// catch-all to larger than 9, or if you need to manually set seed type `S` and final return type `R`
export function flow<S, R>(seed: S, pipeline: ReadonlyArray<(a: S) => any>): R;
export function flow<S, R>(seed: S, pipeline: ReadonlyArray<(a: any) => any>): R;

0 comments on commit ddbfb05

Please sign in to comment.