From 8af698edf949a09ed844e7e3b50ab698b98e712c Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Mon, 3 Jul 2023 11:45:37 -0500 Subject: [PATCH 1/3] feat: "Extends" TypeScript util type #1393 --- packages/utils/src/TypeUtils.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/utils/src/TypeUtils.ts b/packages/utils/src/TypeUtils.ts index 143e6ba456..1b1eb0a0f8 100644 --- a/packages/utils/src/TypeUtils.ts +++ b/packages/utils/src/TypeUtils.ts @@ -1,3 +1,18 @@ +/** + * Util type to create a "subtype" of T. Useful for creating subsets of union + * types. + * + * e.g. + * declare type Direction = 'north' | 'south' | 'east' | 'west' + * + * // This works + * type Down = Subset // 'south' + * + * // Compiler will complain + * type NotDirection = Subset + */ +export type Extends = U; + /** * Util type to extract the value from an object. * From ebe55fa62cede72f81959b41e2871aab0e4c252e Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Mon, 3 Jul 2023 12:32:24 -0500 Subject: [PATCH 2/3] Fixed comment block #1393 --- packages/utils/src/TypeUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/TypeUtils.ts b/packages/utils/src/TypeUtils.ts index 1b1eb0a0f8..c543ee72db 100644 --- a/packages/utils/src/TypeUtils.ts +++ b/packages/utils/src/TypeUtils.ts @@ -6,10 +6,10 @@ * declare type Direction = 'north' | 'south' | 'east' | 'west' * * // This works - * type Down = Subset // 'south' + * type Down = Extends // 'south' * * // Compiler will complain - * type NotDirection = Subset + * type NotDirection = Extends */ export type Extends = U; From 39b203e924428c675d646858f7c3467823413d35 Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Mon, 3 Jul 2023 12:44:34 -0500 Subject: [PATCH 3/3] Constraining type a bit more #1393 --- packages/utils/src/TypeUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/src/TypeUtils.ts b/packages/utils/src/TypeUtils.ts index c543ee72db..fbcef13871 100644 --- a/packages/utils/src/TypeUtils.ts +++ b/packages/utils/src/TypeUtils.ts @@ -11,7 +11,7 @@ * // Compiler will complain * type NotDirection = Extends */ -export type Extends = U; +export type Extends = U extends T ? U : never; /** * Util type to extract the value from an object.