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

feat: add Equal type of Comparison of equivalence #1167

Merged
merged 3 commits into from
Jul 18, 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
3 changes: 2 additions & 1 deletion src/CamelCase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Equal } from "./typings/Equal";

/**
* Camel case type.
*
Expand All @@ -14,7 +16,6 @@ export type CamelCase<T> =
/* -----------------------------------------------------------
OBJECT CONVERSION
----------------------------------------------------------- */
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type CamelizeMain<T> = T extends [never]
? never // special trick for (jsonable | null) type
Expand Down
3 changes: 2 additions & 1 deletion src/PascalCase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Equal } from "./typings/Equal";

/**
* Pascal case type.
*
Expand All @@ -14,7 +16,6 @@ export type PascalCase<T> =
/* -----------------------------------------------------------
OBJECT CONVERSION
----------------------------------------------------------- */
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type PascalizeMain<T> = T extends [never]
? never // special trick for (jsonable | null) type
Expand Down
4 changes: 2 additions & 2 deletions src/Primitive.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Equal } from "./typings/Equal";

/**
* Primitive type of JSON.
*
Expand Down Expand Up @@ -35,8 +37,6 @@
export type Primitive<T> =
Equal<T, PrimitiveMain<T>> extends true ? T : PrimitiveMain<T>;

type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type PrimitiveMain<Instance> = Instance extends [never]
? never // (special trick for jsonable | null) type
: ValueOf<Instance> extends bigint
Expand Down
4 changes: 2 additions & 2 deletions src/Resolved.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Equal } from "./typings/Equal";

/**
* Resolved type erased every methods.
*
Expand Down Expand Up @@ -29,8 +31,6 @@
export type Resolved<T> =
Equal<T, ResolvedMain<T>> extends true ? T : ResolvedMain<T>;

type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type ResolvedMain<T> = T extends [never]
? never // (special trick for jsonable | null) type
: ValueOf<T> extends boolean | number | bigint | string
Expand Down
3 changes: 2 additions & 1 deletion src/SnakeCase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Equal } from "./typings/Equal";

/**
* Snake case type.
*
Expand All @@ -14,7 +16,6 @@ export type SnakeCase<T> =
/* -----------------------------------------------------------
OBJECT CONVERSION
----------------------------------------------------------- */
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type SnakageMain<T> = T extends [never]
? never // special trick for (jsonable | null) type
Expand Down
15 changes: 15 additions & 0 deletions src/typings/Equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Compare the equivalence of the two types X and Y.
* The two types X and Y refer to any type that can be expressed in the type script, such as the union type and the object type.
*
* @template X One of the types to compare
* @template Y One of the types to compare
*
* ```ts
* type Answer = Equal<1 | 2, 1 | 2>; // true
* ```
*/
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
? true
: false;
Loading