-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(flow) add types for new flow function (#122)
- Loading branch information
1 parent
5cea52a
commit 2d03465
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |