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

Re-export ObjectOf, TupleOf and UniformTupleOf types #58

Closed
wants to merge 1 commit into from
Closed
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
62 changes: 48 additions & 14 deletions is/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,18 @@ export function isTupleOf<
}
}

type TupleOf<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];
* ```
*/
Copy link
Author

@kyoh86 kyoh86 Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the previous is.ts in passing and then copied and pasted it, so my comments may not be in line with the new implementation. 🙇

export type TupleOf<T> = {
-readonly [P in keyof T]: T[P] extends Predicate<infer U> ? U : never;
};

Expand Down Expand Up @@ -314,8 +325,19 @@ export function isUniformTupleOf<T, N extends number>(
);
}

/**
* 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
type UniformTupleOf<
export type UniformTupleOf<
T,
N extends number,
R extends readonly T[] = [],
Expand Down Expand Up @@ -536,18 +558,30 @@ function isWithOptional<T extends Predicate<unknown>>(
return isOptional(pred) || (pred as any).optional === true;
}

type ObjectOf<T extends Record<PropertyKey, Predicate<unknown>>> = FlatType<
// Non optional
& {
[K in keyof T as T[K] extends WithOptional ? never : K]: T[K] extends
Predicate<infer U> ? U : never;
}
// Optional
& {
[K in keyof T as T[K] extends WithOptional ? K : never]?: T[K] extends
Predicate<infer U> ? U : never;
}
>;
/**
* 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 Record<PropertyKey, Predicate<unknown>>> =
FlatType<
// Non optional
& {
[K in keyof T as T[K] extends WithOptional ? never : K]: T[K] extends
Predicate<infer U> ? U : never;
}
// Optional
& {
[K in keyof T as T[K] extends WithOptional ? K : never]?: T[K] extends
Predicate<infer U> ? U : never;
}
>;

type IsObjectOfMetadata = {
name: "isObjectOf";
Expand Down
Loading