From b55c88fec5e5d7b754e1dcbc85955159436f56ef Mon Sep 17 00:00:00 2001 From: Rickard Andersson Date: Thu, 3 Aug 2023 11:01:01 +0300 Subject: [PATCH] feat(dev): add `Distinct` type Useful for creating types that are stored as other types (usually primitives) but are represented as distinct in the type system. Canonical example exists in documentation. --- src/types.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/types.ts diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..e896a8a3 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,17 @@ +/** + * A module for utility types that could be used in pretty much every module. + */ + +/** + * Allows one to create a type that is stored as an underlying type but is distinct from it in the + * type system. + * + * @example + * // Creates a distinct `UUID` type from a `string` which can then be expected and only passed + * // with either a `UUID` constructor or a value that comes from elsewhere as a constructed `UUID`. + * type UUID = Distinct<"UUID", string>; + * function UUID(uuid: string): UUID { + * return uuid as UUID; + * } + */ +export type Distinct = Type & { readonly __distinct_tag: Tag };