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 4, 2024
1 parent 9d17dca commit d7867f0
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 63 deletions.
14 changes: 4 additions & 10 deletions packages/core/src/composition/compose/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ExplicitAny, UnaryFunction } from '@/types';
import type { 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 [
...ExplicitAny[],
...any,
infer Last extends UnaryFunction,
]
? // Get Params of the last, which returns [...argTypes], so get the first one [0]
Expand All @@ -20,17 +20,11 @@ type Composable<Fn> =
Fn extends readonly [UnaryFunction]
? Fn
: // if its a list of Unary funcs (ignoring the first)
Fn extends readonly [
ExplicitAny,
...infer Rest extends readonly UnaryFunction[],
]
Fn extends readonly [any, ...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>) => ExplicitAny,
...Composable<Rest>,
]
readonly [(arg: ComposeReturn<Rest>) => any, ...Composable<Rest>]
: never;

/**
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/composition/curry/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// https://github.com/type-challenges/type-challenges/issues/15988

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

export type Curried<T extends unknown[], R> = <P extends Partial<T>>(
...args: P
) => ((...args: T) => ExplicitAny) extends (
...args: [...P, ...infer Args]
) => ExplicitAny
) => ((...args: T) => any) extends (...args: [...P, ...infer Args]) => any
? Args extends []
? R
: Curried<Args, R>
Expand All @@ -22,9 +20,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: ExplicitAny[]) => ExplicitAny>(
export function autoCurry<T extends Callable>(
fn: T,
_args = [] as ExplicitAny[],
_args = [] as any,
): Curried<Parameters<T>, ReturnType<T>> {
return (...__args) =>
((rest) => (rest.length >= fn.length ? fn(...rest) : autoCurry(fn, rest)))([
Expand Down
15 changes: 5 additions & 10 deletions packages/core/src/composition/pipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ExplicitAny, UnaryFunction } from '@/types';
import type { UnaryFunction } from '@/types';

// If its a list of functions, last being Unary
type PipeParams<Fns> = Fns extends readonly [
infer First extends UnaryFunction,
...ExplicitAny[],
...any,
]
? // 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 @@ -14,24 +14,19 @@ 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 [...ExplicitAny[], infer Last extends UnaryFunction]
? Last
: never
Fns extends readonly [...any, infer Last extends UnaryFunction] ? Last : never
>;

type Pipeable<Fn> =
// If it's a single func, just return it
Fn extends readonly [UnaryFunction]
? Fn
: // if its a list of Unary funcs (ignoring the last)
Fn extends readonly [
...infer Head extends readonly UnaryFunction[],
ExplicitAny,
]
Fn extends readonly [...infer Head extends readonly UnaryFunction[], any]
? // 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>) => ExplicitAny]
readonly [...Pipeable<Head>, (arg: PipeReturn<Head>) => any]
: never;

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ 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 Callable = (...all: any) => any;

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type ExplicitAny = any;
export type Comparator<T> = (x: T) => boolean;

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;
export type UnaryFunction = (x: any) => any;
19 changes: 6 additions & 13 deletions packages/core/src/utility/lookup/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ExplicitAny } from '@/types';
import type { Callable } from '@/types';
import { identity } from '../../combinators/i';

type Cache = Record<string | number | symbol, unknown>;

/**
* Takes an object and an optional fallback function and returns a function that
* takes a string and returns the lookup value or the result default fallback.
Expand All @@ -17,15 +19,6 @@ import { identity } from '../../combinators/i';
* colorLookup(data.value);
*/
export const lookup =
<
A extends Record<string | number | symbol, unknown>,
B extends (...args: ExplicitAny[]) => ExplicitAny,
>(
obj: A,
def?: B,
) =>
<C extends keyof A>(prop: string | number | symbol): A[C] => {
const fn = def ?? identity;

return fn(obj[prop]);
};
<A extends Cache, B extends Callable>(obj: A, def?: B) =>
<C extends keyof A>(prop: string | number | symbol): A[C] =>
(def ?? identity)(obj[prop]);
20 changes: 9 additions & 11 deletions packages/core/src/utility/once/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import type { ExplicitAny } from '@/types';

// TS' `Function` type only models the object side of it, not whether it is callable.
type SomeFunction = (...args: ExplicitAny[]) => ExplicitAny;
import type { Callable } from '@/types';

/**
* Ensures that the given function is only called once.
*/
export const once = <T extends SomeFunction>(fn: T) => {
export const once = <T extends Callable>(fn: T) => {
let done = false;

// TODO: Better types, since it can return void?
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
return (...args: Parameters<T>): ReturnType<T> | void =>
// biome-ignore lint/suspicious/noAssignInExpressions: Shhhh
// biome-ignore lint/style/noCommaOperator: Shhh
done ? void 0 : ((done = true), fn(args));
return (...args: Parameters<T>): ReturnType<T> | undefined => {
if (!done) {
done = true;

return fn(...args);
}
};
};
1 change: 0 additions & 1 deletion packages/geo/src/coordinates/coordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type { Tokens } from './latlon/internal/lexer';
import { systemMGRS } from './mgrs/system';
import { systemUTM } from './utm/system';

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type MinLengthArray = [any, any];

type AnySystem = CoordinateSystem<MinLengthArray>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type { ParseResults } from './parse';

// NOTE: isolated CoordinateSystem type so that it could be a private-export

// biome-ignore lint/suspicious/noExplicitAny: there must be a better way???
export type CoordinateSystem<F = any> = {
name: string;
parse: (format: Format, input: string) => ParseResults;
Expand Down
1 change: 0 additions & 1 deletion packages/geo/src/coordinates/mgrs/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ function detailedErrors(input: string) {
}

// biome-ignore lint/style/useNamingConvention: <explanation>
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function parseMGRS(_format: any, input: string) {
try {
const point = MGRS.parse(input).toPoint();
Expand Down
1 change: 0 additions & 1 deletion packages/geo/src/coordinates/utm/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function detailedErrors(input: string) {
}

// biome-ignore lint/style/useNamingConvention: <explanation>
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function parseUTM(_format: any, input: string) {
try {
const point = UTM.parse(input).toPoint();
Expand Down
1 change: 0 additions & 1 deletion packages/predicates/src/is-noyes/index.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import { bench, describe } from 'vitest';

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type Callable = (...a: any[]) => any;

const TEST_VALUES = [
Expand Down
3 changes: 0 additions & 3 deletions packages/web-worker/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ let i = 0;

const uuid = (namespace: string) => `${namespace}_${++i}`;

// biome-ignore lint/suspicious/noExplicitAny: Figure out type hinting for "any" values
type Actions = Record<string, (...args: any[]) => any>;

// biome-ignore lint/suspicious/noExplicitAny: Figure out type hinting for "any" values
type MessageData<T> = { id: string; type: T; args: any[] };

// biome-ignore lint/suspicious/noExplicitAny: Figure out type hinting for "any" values
type MessageDataResult = { id: string; ok?: any; error?: any };

type Events = Record<string, MessageDataResult>;
Expand Down
2 changes: 1 addition & 1 deletion tooling/biome-config/linter.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"noArrayIndexKey": "warn",
"noEmptyBlockStatements": "error",
"useAwait": "error",
"noExplicitAny": "warn"
"noExplicitAny": "off"
},
"nursery": {
"noDuplicateElseIf": "error"
Expand Down

0 comments on commit d7867f0

Please sign in to comment.