Skip to content

Commit

Permalink
Merge pull request #1167 from kakasoo/master
Browse files Browse the repository at this point in the history
feat: add `Equal` type of Comparison of equivalence
  • Loading branch information
samchon committed Jul 18, 2024
2 parents 66bf41d + 8db3cff commit a656281
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
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;

0 comments on commit a656281

Please sign in to comment.