From c5ef36d0884a7ef818a3e65edb08d9f3fe5977c0 Mon Sep 17 00:00:00 2001 From: kakasoo Date: Wed, 17 Jul 2024 10:54:05 +0900 Subject: [PATCH 1/3] feat: add `Equal` type of Comparison of equivalence --- src/typings/Equal.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/typings/Equal.ts diff --git a/src/typings/Equal.ts b/src/typings/Equal.ts new file mode 100644 index 0000000000..be5c7a0901 --- /dev/null +++ b/src/typings/Equal.ts @@ -0,0 +1,12 @@ +/** + * In order to determine whether it is the same in the union type, + * the types are compared by wrapping them with a function with a generic type as a factor once more. + * + * ```ts + * type Answer = Equal<1 | 2, 1 | 2>; // true + * ``` + */ +type Equal = + (() => T extends X ? 1 : 2) extends () => T extends Y ? 1 : 2 + ? true + : false; From 524299de26eb983bdec3ecfe58fed846a5733d55 Mon Sep 17 00:00:00 2001 From: kakasoo Date: Wed, 17 Jul 2024 11:03:37 +0900 Subject: [PATCH 2/3] feat: import `Equal` type from typings --- src/CamelCase.ts | 1 - src/PascalCase.ts | 1 - src/Primitive.ts | 2 -- src/Resolved.ts | 2 -- src/SnakeCase.ts | 1 - 5 files changed, 7 deletions(-) diff --git a/src/CamelCase.ts b/src/CamelCase.ts index 4cf7945b31..af81675d00 100644 --- a/src/CamelCase.ts +++ b/src/CamelCase.ts @@ -14,7 +14,6 @@ export type CamelCase = /* ----------------------------------------------------------- OBJECT CONVERSION ----------------------------------------------------------- */ -type Equal = X extends Y ? (Y extends X ? true : false) : false; type CamelizeMain = T extends [never] ? never // special trick for (jsonable | null) type diff --git a/src/PascalCase.ts b/src/PascalCase.ts index eca17e340a..0649a86bd2 100644 --- a/src/PascalCase.ts +++ b/src/PascalCase.ts @@ -14,7 +14,6 @@ export type PascalCase = /* ----------------------------------------------------------- OBJECT CONVERSION ----------------------------------------------------------- */ -type Equal = X extends Y ? (Y extends X ? true : false) : false; type PascalizeMain = T extends [never] ? never // special trick for (jsonable | null) type diff --git a/src/Primitive.ts b/src/Primitive.ts index e500dca18d..233dbbb0c2 100644 --- a/src/Primitive.ts +++ b/src/Primitive.ts @@ -35,8 +35,6 @@ export type Primitive = Equal> extends true ? T : PrimitiveMain; -type Equal = X extends Y ? (Y extends X ? true : false) : false; - type PrimitiveMain = Instance extends [never] ? never // (special trick for jsonable | null) type : ValueOf extends bigint diff --git a/src/Resolved.ts b/src/Resolved.ts index c15627882d..da27f662bc 100644 --- a/src/Resolved.ts +++ b/src/Resolved.ts @@ -29,8 +29,6 @@ export type Resolved = Equal> extends true ? T : ResolvedMain; -type Equal = X extends Y ? (Y extends X ? true : false) : false; - type ResolvedMain = T extends [never] ? never // (special trick for jsonable | null) type : ValueOf extends boolean | number | bigint | string diff --git a/src/SnakeCase.ts b/src/SnakeCase.ts index 96f9fd9e14..239c597870 100644 --- a/src/SnakeCase.ts +++ b/src/SnakeCase.ts @@ -14,7 +14,6 @@ export type SnakeCase = /* ----------------------------------------------------------- OBJECT CONVERSION ----------------------------------------------------------- */ -type Equal = X extends Y ? (Y extends X ? true : false) : false; type SnakageMain = T extends [never] ? never // special trick for (jsonable | null) type From 8db3cff76c744d5c7e28016bb38a3f2be482db2a Mon Sep 17 00:00:00 2001 From: kakasoo Date: Thu, 18 Jul 2024 11:05:05 +0900 Subject: [PATCH 3/3] fix: Add import, export for Equal type according to feedback --- src/CamelCase.ts | 2 ++ src/PascalCase.ts | 2 ++ src/Primitive.ts | 2 ++ src/Resolved.ts | 2 ++ src/SnakeCase.ts | 2 ++ src/typings/Equal.ts | 9 ++++++--- 6 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/CamelCase.ts b/src/CamelCase.ts index af81675d00..516c5be7a4 100644 --- a/src/CamelCase.ts +++ b/src/CamelCase.ts @@ -1,3 +1,5 @@ +import type { Equal } from "./typings/Equal"; + /** * Camel case type. * diff --git a/src/PascalCase.ts b/src/PascalCase.ts index 0649a86bd2..75dadb45e4 100644 --- a/src/PascalCase.ts +++ b/src/PascalCase.ts @@ -1,3 +1,5 @@ +import type { Equal } from "./typings/Equal"; + /** * Pascal case type. * diff --git a/src/Primitive.ts b/src/Primitive.ts index 233dbbb0c2..b791c9a7d7 100644 --- a/src/Primitive.ts +++ b/src/Primitive.ts @@ -1,3 +1,5 @@ +import type { Equal } from "./typings/Equal"; + /** * Primitive type of JSON. * diff --git a/src/Resolved.ts b/src/Resolved.ts index da27f662bc..1f791245ac 100644 --- a/src/Resolved.ts +++ b/src/Resolved.ts @@ -1,3 +1,5 @@ +import type { Equal } from "./typings/Equal"; + /** * Resolved type erased every methods. * diff --git a/src/SnakeCase.ts b/src/SnakeCase.ts index 239c597870..e099033c40 100644 --- a/src/SnakeCase.ts +++ b/src/SnakeCase.ts @@ -1,3 +1,5 @@ +import type { Equal } from "./typings/Equal"; + /** * Snake case type. * diff --git a/src/typings/Equal.ts b/src/typings/Equal.ts index be5c7a0901..71f6b7eca3 100644 --- a/src/typings/Equal.ts +++ b/src/typings/Equal.ts @@ -1,12 +1,15 @@ /** - * In order to determine whether it is the same in the union type, - * the types are compared by wrapping them with a function with a generic type as a factor once more. + * 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 * ``` */ -type Equal = +export type Equal = (() => T extends X ? 1 : 2) extends () => T extends Y ? 1 : 2 ? true : false;