Skip to content
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

new(flow) add types for new flow function #122

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
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
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;
Loading