Skip to content

Commit

Permalink
new(flow) add types for new flow function (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller authored May 2, 2024
1 parent 5cea52a commit 2d03465
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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));
11 changes: 11 additions & 0 deletions types/flow.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function flow<S, R1>(seed: S, pipeline: [(a: S) => R1]): R1;
export function flow<S, R1, R2>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2]): R2;
export function flow<S, R1, R2, R3>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3]): R3;
export function flow<S, R1, R2, R3, R4>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4]): R4;
export function flow<S, R1, R2, R3, R4, R5>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5]): R5;
export function flow<S, R1, R2, R3, R4, R5, R6>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5, (a: R5) => R6]): R6;
export function flow<S, R1, R2, R3, R4, R5, R6, R7>(seed: S, pipeline: [(a: S) => R1, (a: R1) => R2, (a: R2) => R3, (a: R3) => R4, (a: R4) => R5, (a: R5) => R6, (a: R6) => R7]): R7;
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: any) => any>): R;

0 comments on commit 2d03465

Please sign in to comment.