Skip to content

Commit

Permalink
💥 Remove some internal types from the public
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Feb 12, 2024
1 parent c77a28b commit 34d2a04
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 125 deletions.
84 changes: 12 additions & 72 deletions is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,33 +233,11 @@ export function isSetOf<T>(
);
}

/**
* Tuple type of types that are predicated by an array of predicate functions.
*
* ```ts
* import { is, TupleOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
*
* type A = TupleOf<readonly [typeof is.String, typeof is.Number]>;
* // Above is equivalent to the following type
* // type A = [string, number];
* ```
*/
export type TupleOf<T> = {
type TupleOf<T> = {
-readonly [P in keyof T]: T[P] extends Predicate<infer U> ? U : never;
};

/**
* Readonly tuple type of types that are predicated by an array of predicate functions.
*
* ```ts
* import { is, ReadonlyTupleOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
*
* type A = ReadonlyTupleOf<readonly [typeof is.String, typeof is.Number]>;
* // Above is equivalent to the following type
* // type A = readonly [string, number];
* ```
*/
export type ReadonlyTupleOf<T> = {
type ReadonlyTupleOf<T> = {
[P in keyof T]: T[P] extends Predicate<infer U> ? U : never;
};

Expand Down Expand Up @@ -454,36 +432,14 @@ export function isReadonlyTupleOf<
}
}

/**
* Uniform tuple type of types that are predicated by a predicate function and the length is `N`.
*
* ```ts
* import { is, UniformTupleOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
*
* type A = UniformTupleOf<number, 5>;
* // Above is equivalent to the following type
* // type A = [number, number, number, number, number];
* ```
*/
// https://stackoverflow.com/a/71700658/1273406
export type UniformTupleOf<
type UniformTupleOf<
T,
N extends number,
R extends readonly T[] = [],
> = R["length"] extends N ? R : UniformTupleOf<T, N, [T, ...R]>;

/**
* Readonly uniform tuple type of types that are predicated by a predicate function `T` and the length is `N`.
*
* ```ts
* import { is, ReadonlyUniformTupleOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
*
* type A = ReadonlyUniformTupleOf<number, 5>;
* // Above is equivalent to the following type
* // type A = readonly [number, number, number, number, number];
* ```
*/
export type ReadonlyUniformTupleOf<
type ReadonlyUniformTupleOf<
T,
N extends number,
R extends readonly T[] = [],
Expand Down Expand Up @@ -581,11 +537,6 @@ export function isReadonlyUniformTupleOf<T, N extends number>(
);
}

/**
* Synonym of `Record<K, T>`
*/
export type RecordOf<T, K extends PropertyKey = PropertyKey> = Record<K, T>;

/**
* Return `true` if the type of `x` is `Record<PropertyKey, unknown>`.
*
Expand Down Expand Up @@ -732,22 +683,11 @@ export function isMapOf<T, K>(
);
}

type OptionalPredicateKeys<T extends RecordOf<unknown>> = {
type OptionalPredicateKeys<T extends Record<PropertyKey, unknown>> = {
[K in keyof T]: T[K] extends OptionalPredicate<unknown> ? K : never;
}[keyof T];

/**
* Object types that are predicated by predicate functions in the object `T`.
*
* ```ts
* import { is, ObjectOf } from "https://deno.land/x/unknownutil@$MODULE_VERSION/mod.ts";
*
* type A = ObjectOf<{ a: typeof is.Number, b: typeof is.String }>;
* // Above is equivalent to the following type
* // type A = { a: number; b: string };
* ```
*/
export type ObjectOf<T extends RecordOf<Predicate<unknown>>> = FlatType<
type ObjectOf<T extends Record<PropertyKey, Predicate<unknown>>> = FlatType<
& {
[K in Exclude<keyof T, OptionalPredicateKeys<T>>]: T[K] extends
Predicate<infer U> ? U : never;
Expand Down Expand Up @@ -801,7 +741,7 @@ export type ObjectOf<T extends RecordOf<Predicate<unknown>>> = FlatType<
* ```
*/
export function isObjectOf<
T extends RecordOf<Predicate<unknown>>,
T extends Record<PropertyKey, Predicate<unknown>>,
>(
predObj: T,
{ strict }: { strict?: boolean } = {},
Expand All @@ -817,7 +757,7 @@ export function isObjectOf<
}

function isObjectOfLoose<
T extends RecordOf<Predicate<unknown>>,
T extends Record<PropertyKey, Predicate<unknown>>,
>(
predObj: T,
): Predicate<ObjectOf<T>> {
Expand All @@ -831,7 +771,7 @@ function isObjectOfLoose<
}

function isObjectOfStrict<
T extends RecordOf<Predicate<unknown>>,
T extends Record<PropertyKey, Predicate<unknown>>,
>(
predObj: T,
): Predicate<ObjectOf<T>> {
Expand Down Expand Up @@ -1081,7 +1021,7 @@ export function isLiteralOneOf<T extends readonly Primitive[]>(
);
}

export type OneOf<T> = T extends readonly [Predicate<infer U>, ...infer R]
type OneOf<T> = T extends readonly [Predicate<infer U>, ...infer R]
? U | OneOf<R>
: never;

Expand Down Expand Up @@ -1131,7 +1071,7 @@ export function isOneOf<
);
}

export type AllOf<T> = UnionToIntersection<OneOf<T>>;
type AllOf<T> = UnionToIntersection<OneOf<T>>;

/**
* Return a type predicate function that returns `true` if the type of `x` is `AllOf<T>`.
Expand Down Expand Up @@ -1185,7 +1125,7 @@ export function isAllOf<
);
}

export type OptionalPredicate<T> = Predicate<T | undefined> & {
type OptionalPredicate<T> = Predicate<T | undefined> & {
optional: true;
};

Expand Down
53 changes: 0 additions & 53 deletions is_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ import is, {
isUndefined,
isUniformTupleOf,
isUnknown,
ObjectOf,
Predicate,
PredicateType,
ReadonlyTupleOf,
ReadonlyUniformTupleOf,
TupleOf,
UniformTupleOf,
} from "./is.ts";

// It seems 'IsExact' in deno_std is false positive so use `Equal` in type-challenges
Expand Down Expand Up @@ -252,24 +247,6 @@ Deno.test("isSetOf<T>", async (t) => {
});
});

Deno.test("TupleOf<T>", () => {
assertType<
Equal<
TupleOf<readonly [typeof is.String, typeof is.Number]>,
[string, number]
>
>(true);
});

Deno.test("ReadonlyTupleOf<T>", () => {
assertType<
Equal<
ReadonlyTupleOf<readonly [typeof is.String, typeof is.Number]>,
readonly [string, number]
>
>(true);
});

Deno.test("isTupleOf<T>", async (t) => {
await t.step("returns properly named function", async (t) => {
await assertSnapshot(
Expand Down Expand Up @@ -482,27 +459,6 @@ Deno.test("isReadonlyTupleOf<T, E>", async (t) => {
);
});

Deno.test("UniformTupleOf<N, T>", () => {
assertType<
Equal<UniformTupleOf<number, 5>, [number, number, number, number, number]>
>(true);
});

Deno.test("ReadonlyUniformTupleOf<N, T>", () => {
assertType<
Equal<
ReadonlyUniformTupleOf<number, 5>,
readonly [
number,
number,
number,
number,
number,
]
>
>(true);
});

Deno.test("isUniformTupleOf<T>", async (t) => {
await t.step("returns properly named function", async (t) => {
await assertSnapshot(t, isUniformTupleOf(3).name);
Expand Down Expand Up @@ -715,15 +671,6 @@ Deno.test("isMapOf<T, K>", async (t) => {
});
});

Deno.test("ObjectOf<T>", () => {
assertType<
Equal<
ObjectOf<{ a: typeof is.Number; b: typeof is.String }>,
{ a: number; b: string }
>
>(true);
});

Deno.test("isObjectOf<T>", async (t) => {
await t.step("returns properly named function", async (t) => {
await assertSnapshot(
Expand Down

0 comments on commit 34d2a04

Please sign in to comment.