From d5f1a703fcf4136abee27f489981e80295f0a992 Mon Sep 17 00:00:00 2001 From: uri Date: Wed, 8 Jan 2025 11:22:32 +0200 Subject: [PATCH] improve-pipeline-type-inference --- src/composition.test.ts | 9 ++++++--- src/composition.ts | 12 +++++------- src/io.ts | 1 + src/map.ts | 7 ++----- src/typing.ts | 4 ++++ 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/composition.test.ts b/src/composition.test.ts index 7c0f79b..819480e 100644 --- a/src/composition.test.ts +++ b/src/composition.test.ts @@ -96,10 +96,13 @@ const _3: Promise = pipe( (y: number) => y, )(1); -// failing typing tests: +const f = pipe((x: T) => x); +f(8); + +// Generics inference +const _4: number = pipe((y: number) => y, (x: T) => x)(1); -// Generics can infer by the last function -// const _4: number = pipe((y: number) => y, (x: T) => x)(1); +// failing typing tests: // Generics understands contextual extends // const _5 = number>(f: Fn) => { diff --git a/src/composition.ts b/src/composition.ts index 9ee5b32..e83054d 100644 --- a/src/composition.ts +++ b/src/composition.ts @@ -1,4 +1,5 @@ import { reverse } from "./array.ts"; +import { ComposeMany } from "./composeTyping.ts"; import { not } from "./operator.ts"; import { isPromise } from "./promise.ts"; import { reduce } from "./reduce.ts"; @@ -7,9 +8,8 @@ import type { AnyAsync, AsyncFunction, Func, - Last, ParamOf, - ReturnTypeUnwrapped, + PromisifyFunction, UnaryFnUntyped, } from "./typing.ts"; @@ -30,10 +30,9 @@ type ValidPipe = FS extends ? [ValidCompose, ...ValidPipe<[F2, ...Rest]>] // tuple length >= 2 : FS; // tuple length < 2 -type Pipeline = ( - ...x: Parameters -) => Fs extends AnyAsync ? Promise>> - : ReturnType>; +type Pipeline = Fs extends AnyAsync + ? PromisifyFunction> + : ComposeMany; const pipeWithoutStack = ( ...fs: ValidPipe @@ -86,7 +85,6 @@ type Reversed = Tuple extends [infer Head, ...infer Rest] export const compose = ( ...fs: Fs ): Fs extends ValidPipe> ? Pipeline> : never => - // @ts-expect-error reason: TODO - fix typing pipe(...reverse(fs)); export const after = diff --git a/src/io.ts b/src/io.ts index b27e4ce..7007b6a 100644 --- a/src/io.ts +++ b/src/io.ts @@ -64,6 +64,7 @@ export const batch = < const keyToTimeout: Record = {} as Record; const clearAndExecute = (key: TaskKey) => { + // @ts-expect-error not sure what's wrong here executeTasks(execute)(keyToTasks[key]); clearTimeout(keyToTimeout[key]); delete keyToTimeout[key]; diff --git a/src/map.ts b/src/map.ts index 55c1de6..f52fa02 100644 --- a/src/map.ts +++ b/src/map.ts @@ -32,8 +32,5 @@ export const each = (f: F) => if (results.length) return Promise.all(results).then(); }; -export const mapCat = ( - f: Unary, -) => -// @ts-expect-error ts cannot reason about this -(x: T[]): G => pipe(map(f), reduce((a, b) => a.concat(b), () => []))(x); +export const mapCat = (f: Unary) => (x: T[]): G => + pipe(map(f), reduce((a, b) => a.concat(b), () => []))(x); diff --git a/src/typing.ts b/src/typing.ts index 4fd4cb4..c25a08d 100644 --- a/src/typing.ts +++ b/src/typing.ts @@ -44,3 +44,7 @@ export type ReturnTypeUnwrapped = F extends AsyncFunction // deno-lint-ignore no-explicit-any export type UnaryFnUntyped = (input: any) => any; + +export type PromisifyFunction = ( + ...args: Parameters +) => Promise>;