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

Tighten function types #156

Merged
merged 2 commits into from
Jun 24, 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
13 changes: 10 additions & 3 deletions src/internal/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// deno-lint-ignore-file no-namespace ban-ts-comment
import { Composable } from '../index.ts'
import { assertEquals, describe, it } from '../tests/prelude.ts'
import type { Internal } from './types.ts'

Expand Down Expand Up @@ -253,12 +254,18 @@ namespace Prettify {
namespace ApplyArgumentsToFns {
type WithEmpty = Expect<Equal<Internal.ApplyArgumentsToFns<[], [string]>, []>>
type WithSingle = Expect<
Equal<Internal.ApplyArgumentsToFns<[() => 1], [string]>, [(a: string) => 1]>
Equal<
Internal.ApplyArgumentsToFns<[Composable<() => 1>], [string]>,
[Composable<(a: string) => 1>]
>
>
type WithMultiple = Expect<
Equal<
Internal.ApplyArgumentsToFns<[() => 1, (a: 1) => 'a'], [string]>,
[(a: string) => 1, (a: string) => 'a']
Internal.ApplyArgumentsToFns<
[Composable<() => 1>, Composable<(a: 1) => 'a'>],
[string]
>,
[Composable<(a: string) => 1>, Composable<(a: string) => 'a'>]
>
>
}
Expand Down
16 changes: 11 additions & 5 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// deno-lint-ignore-file no-namespace

import { Composable } from '../types.ts'

namespace Internal {
export type IncompatibleArguments = {
'Incompatible arguments ': true
Expand Down Expand Up @@ -29,11 +31,15 @@ namespace Internal {
: false

export type ApplyArgumentsToFns<
Fns extends any[],
Args extends any[],
Output extends any[] = [],
> = Fns extends [(...a: any[]) => infer OA, ...infer restA]
? ApplyArgumentsToFns<restA, Args, [...Output, (...a: Args) => OA]>
Fns extends unknown[],
Args extends unknown[],
Output extends Composable[] = [],
> = Fns extends [Composable<(...a: any[]) => infer OA>, ...infer restA]
? ApplyArgumentsToFns<
restA,
Args,
[...Output, Composable<(...a: Args) => OA>]
>
: Output

// Thanks to https://github.com/tjjfvi
Expand Down
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ type PipeReturn<Fns extends unknown[]> = Fns extends [
* Determines whether a sequence of Composables can be composed sequentially.
*/
type CanComposeInSequence<
Fns extends any[],
Arguments extends any[] = [],
Fns extends unknown[],
Arguments extends unknown[] = [],
> = Fns extends [Composable<(...a: infer PA) => infer OA>, ...infer restA]
? restA extends [
Composable<
Expand All @@ -122,9 +122,9 @@ type CanComposeInSequence<
* Determines whether a sequence of Composables can be composed in parallel.
*/
type CanComposeInParallel<
Fns extends any[],
OriginalFns extends any[] = Fns,
> = Fns extends [Composable<(...a: infer PA) => any>, ...infer restA]
Fns extends unknown[],
OriginalFns extends unknown[] = Fns,
> = Fns extends [Composable<(...a: infer PA) => unknown>, ...infer restA]
? restA extends [Composable<(...b: infer PB) => infer OB>, ...infer restB]
? Internal.SubtypesTuple<PA, PB> extends [...infer MergedP]
? CanComposeInParallel<
Expand Down Expand Up @@ -228,7 +228,7 @@ type ApplySchemaReturn<
*/
type MapParametersReturn<
Fn extends Composable,
NewParams extends any[],
NewParams extends unknown[],
O extends Parameters<Fn>,
> = Composable<
(
Expand Down
Loading