Guid TypeScript is a lightweight JavaScript library focused on generating RFC4122-compliant GUIDs (also known as UUIDs) specifically for TypeScript environments. It is currently at version 1.0.9, suggesting a stable codebase with a focus on maintenance rather than rapid feature development. The library provides a `Guid` class that encapsulates GUID values, offering both static methods for creating new GUIDs (`create`, `createEmpty`, `raw`), parsing strings into `Guid` instances (`parse`), and validating potential GUID values (`isGuid`). Additionally, `Guid` instances include methods for comparison (`equals`), emptiness checks (`isEmpty`), and conversion to string or JSON formats (`toString`, `toJSON`). Its key differentiator lies in providing strong TypeScript typing, which allows developers to leverage type safety when working with GUIDs as objects, rather than managing them as raw strings. This approach helps prevent common errors associated with string manipulation and ensures consistency across an application's data models.
npm install guid-typescriptVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic GUID creation, checking for emptiness, comparing GUIDs, parsing a string into a Guid instance, and validating GUIDs within a TypeScript class context.
Use `myGuidObject.toString() === 'some-guid-string'` or `Guid.isGuid(potentialGuidString)` for comparisons involving strings.
Wrap `Guid.parse()` calls in a `try...catch` block, or validate the string first using `Guid.isGuid()` before attempting to parse: `if (Guid.isGuid(myString)) { try { Guid.parse(myString); } catch (e) { /* handle error */ } }`Explicitly define ID properties as `id: Guid` and use `Guid.create()`, `Guid.parse()`, or `.toString()` when interacting with string-based APIs.
Use the static factory method `Guid.create()` to generate new GUIDs, and ensure `import { Guid } from 'guid-typescript';` or `const { Guid } = require('guid-typescript');`.Ensure `Guid` is imported as a named export (`import { Guid } from 'guid-typescript';`) and call the method as `Guid.create()`.Validate the input string format using `Guid.isGuid(myString)` before calling `Guid.parse(myString)`. If the string is not valid, handle the error gracefully.
No dependency data recorded yet.