Skip to content

Commit

Permalink
refactor: make anys explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
kalisjoshua committed Dec 3, 2024
1 parent 6c23f31 commit 612937f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/composition/compose/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { UnaryFunction } from '@/types';
import type { ExplicitAny, UnaryFunction } from '@/types';

// https://stackoverflow.com/questions/49310886/typing-compose-function-in-typescript-flow-compose#answer-73082627

// If its a list of functions, last being Unary
type ComposeParams<Fns> = Fns extends readonly [
...any[],
...ExplicitAny[],
infer Last extends UnaryFunction,
]
? // Get Params of the last, which returns [...argTypes], so get the first one [0]
Expand All @@ -21,11 +21,17 @@ type Composable<Fn> =
Fn extends readonly [UnaryFunction]
? Fn
: // if its a list of Unary funcs (ignoring the first)
Fn extends readonly [any, ...infer Rest extends readonly UnaryFunction[]]
Fn extends readonly [
ExplicitAny,
...infer Rest extends readonly UnaryFunction[],
]
? // Start building the list of func type by using the return type of the first in Rest
// as the arg of the next in line and recursively spread the rest (doing the same thing)
// The first is ignored but handled by the top level ComposeReturn
readonly [(arg: ComposeReturn<Rest>) => any, ...Composable<Rest>]
readonly [
(arg: ComposeReturn<Rest>) => ExplicitAny,
...Composable<Rest>,
]
: never;

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/composition/curry/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// https://github.com/type-challenges/type-challenges/issues/15988

import type { ExplicitAny } from '@/types';

export type Curried<T extends unknown[], R> = <P extends Partial<T>>(
...args: P
) => ((...args: T) => any) extends (...args: [...P, ...infer Args]) => any
) => ((...args: T) => ExplicitAny) extends (
...args: [...P, ...infer Args]
) => ExplicitAny
? Args extends []
? R
: Curried<Args, R>
Expand All @@ -19,9 +22,9 @@ export type Curried<T extends unknown[], R> = <P extends Partial<T>>(
* curried(2)(3, 4);
* curried(2, 3, 4);
*/
export function autoCurry<T extends (...args: any[]) => any>(
export function autoCurry<T extends (...args: ExplicitAny[]) => ExplicitAny>(
fn: T,
_args = [] as any[],
_args = [] as ExplicitAny[],
): Curried<Parameters<T>, ReturnType<T>> {
return (...__args) =>
((rest) => (rest.length >= fn.length ? fn(...rest) : autoCurry(fn, rest)))([
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/composition/pipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { UnaryFunction } from '@/types';
import type { ExplicitAny, UnaryFunction } from '@/types';

// If its a list of functions, last being Unary
type PipeParams<Fns> = Fns extends readonly [
infer First extends UnaryFunction,
...any[],
...ExplicitAny[],
]
? // Get Params of the first, which returns [...argTypes], so get the first one [0]
// so that we have the true type of the arg
Expand All @@ -15,7 +14,7 @@ type PipeParams<Fns> = Fns extends readonly [
// have to spread and infer last so that it gets the right type for the last one
// [-1] no bueno
type PipeReturn<Fns> = ReturnType<
Fns extends readonly [...any[], infer Last extends UnaryFunction]
Fns extends readonly [...ExplicitAny[], infer Last extends UnaryFunction]
? Last
: never
>;
Expand All @@ -25,11 +24,14 @@ type Pipeable<Fn> =
Fn extends readonly [UnaryFunction]
? Fn
: // if its a list of Unary funcs (ignoring the last)
Fn extends readonly [...infer Head extends readonly UnaryFunction[], any]
Fn extends readonly [
...infer Head extends readonly UnaryFunction[],
ExplicitAny,
]
? // Start building the list of func type by using the return type of the last in Head
// as the arg of the previous in line and recursively spread the rest (doing the same thing)
// The last is ignored but handled by the top level FlowReturn
readonly [...Pipeable<Head>, (arg: PipeReturn<Head>) => any]
readonly [...Pipeable<Head>, (arg: PipeReturn<Head>) => ExplicitAny]
: never;

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export type UnaryFunction = (x: any) => any;
export type Accumulator<T, R> = (acc: R, x: T) => R;

export type ArrayElementType<T> = T extends (infer E)[] ? E : T;

export type Comparator<T> = (x: T) => boolean;

export type Predicate<T> = (x: T, idx?: number) => boolean;

export type Accumulator<T, R> = (acc: R, x: T) => R;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type ExplicitAny = any;

export type MapFn<T, R> = (x: T, idx?: number) => R;

export type Predicate<T> = (x: T, idx?: number) => boolean;

export type UnaryFunction = (x: ExplicitAny) => ExplicitAny;
3 changes: 2 additions & 1 deletion packages/core/src/utility/lookup/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExplicitAny } from '@/types';
import { identity } from '../../combinators/i';

/**
Expand All @@ -18,7 +19,7 @@ import { identity } from '../../combinators/i';
export const lookup =
<
A extends Record<string | number | symbol, unknown>,
B extends (...args: any[]) => any,
B extends (...args: ExplicitAny[]) => ExplicitAny,
>(
obj: A,
def?: B,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/utility/once/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ExplicitAny } from '@/types';

// TS' `Function` type only models the object side of it, not whether it is callable.
type SomeFunction = (...args: any[]) => any;
type SomeFunction = (...args: ExplicitAny[]) => ExplicitAny;

/**
* Ensures that the given function is only called once.
Expand Down

0 comments on commit 612937f

Please sign in to comment.