Skip to content

Commit

Permalink
Adding expolicit return types and remobing explicit casts whenever po…
Browse files Browse the repository at this point in the history
…ssible
  • Loading branch information
diogob committed May 29, 2024
1 parent 8edb288 commit 27505f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ function mergeObjects<T extends unknown[] = unknown[]>(
* // ^? Composable<({ aNumber }: { aNumber: number }) => { aBoolean: boolean }>
* ```
*/
function pipe<Fns extends [Composable, ...Composable[]]>(...fns: Fns) {
function pipe<Fns extends [Composable, ...Composable[]]>(
...fns: Fns
): PipeReturn<CanComposeInSequence<Fns>> {
const last = <T extends any[]>(arr: T): Last<T> => arr.at(-1)
return map(sequence(...fns), last as never) as PipeReturn<
CanComposeInSequence<Fns>
Expand All @@ -89,7 +91,13 @@ function pipe<Fns extends [Composable, ...Composable[]]>(...fns: Fns) {
* // ^? Composable<(id: number) => [string, number, boolean]>
* ```
*/
function all<Fns extends Composable[]>(...fns: Fns) {
function all<Fns extends Composable[]>(
...fns: Fns
): Composable<
(...args: Parameters<NonNullable<CanComposeInParallel<Fns>[0]>>) => {
[k in keyof Fns]: UnpackData<Fns[k]>
}
> {
return (async (...args) => {
const results = await Promise.all(fns.map((fn) => fn(...args)))

Expand Down Expand Up @@ -119,7 +127,17 @@ function all<Fns extends Composable[]>(...fns: Fns) {
* // ^? Composable<() => { a: string, b: number }>
* ```
*/
function collect<Fns extends Record<string, Composable>>(fns: Fns) {
function collect<Fns extends Record<string, Composable>>(
fns: Fns,
): Composable<
(
...args: Parameters<
Exclude<CanComposeInParallel<RecordToTuple<Fns>>[0], undefined>
>
) => {
[key in keyof Fns]: UnpackData<Fns[key]>
}
> {
const fnsWithKey = Object.entries(fns).map(([key, cf]) =>
map(cf, (result) => ({ [key]: result })),
)
Expand Down
9 changes: 6 additions & 3 deletions src/environment/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function applyEnvironmentToList<
* // ^? ComposableWithSchema<{ aBoolean: boolean }>
* ```
*/
function pipe<Fns extends Composable[]>(...fns: Fns) {
function pipe<Fns extends Composable[]>(...fns: Fns): PipeReturn<Fns> {
return ((input: any, environment: any) =>
A.pipe(...applyEnvironmentToList(fns, environment))(
input,
Expand All @@ -49,7 +49,7 @@ function pipe<Fns extends Composable[]>(...fns: Fns) {
* ```
*/

function sequence<Fns extends Composable[]>(...fns: Fns) {
function sequence<Fns extends Composable[]>(...fns: Fns): SequenceReturn<Fns> {
return ((input: any, environment: any) =>
A.sequence(...applyEnvironmentToList(fns, environment))(
input,
Expand All @@ -64,7 +64,10 @@ function branch<
Resolver extends (
o: UnpackData<SourceComposable>,
) => Composable | null | Promise<Composable | null>,
>(cf: SourceComposable, resolver: Resolver) {
>(
cf: SourceComposable,
resolver: Resolver,
): BranchReturn<SourceComposable, Resolver> {
return (async (...args: Parameters<SourceComposable>) => {
const [input, environment] = args
const result = await cf(input, environment)
Expand Down

0 comments on commit 27505f1

Please sign in to comment.