Developer Documentation
TypeScript
Updated September 19, 2026

Generate and Work with GUIDs in TypeScript

Generate, parse, format, and validate GUID/UUID values in TypeScript.

A GUID is Microsoft terminology for a 128-bit globally unique identifier. In practice, the canonical textual form is compatible with UUID formatting, although platform APIs may expose additional GUID-specific formats.

Quick Start#

The following example uses the standard TypeScript API or a widely used package for this task.

Quick start#

TypeScript
1const id: string = crypto.randomUUID();2console.log(id);

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Typed validation#

TypeScript
1type Guid = string & { readonly __guid: unique symbol };2const GUID_RE = /^[--f]{}-[--f]{}-[-][--f]{}-[][--f]{}-[--f]{}$/i;34function asGuid(value: string): Guid {5  if (!GUID_RE.test(value)) throw new TypeError("Invalid GUID");6  return value as Guid;7}89const id = asGuid(crypto.randomUUID());

Important Notes#

Branded types can prevent accidental swapping of unrelated identifiers while keeping runtime values as strings.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement GUID in other languages: