{"id":12236,"library":"typescript-guid","title":"Guid Typescript","description":"typescript-guid is a lightweight utility library for generating, parsing, and manipulating Globally Unique Identifiers (GUIDs), also commonly known as UUIDs, within TypeScript and JavaScript environments. The current stable version is 1.0.3. The library offers a comprehensive set of static methods for creating new GUIDs, including empty GUIDs, parsing GUID strings into `Guid` objects, and validating arbitrary values against the GUID format. `Guid` instances support comparisons (case-insensitive since v1.0.1), emptiness checks, and serialization to string or JSON formats. It is a direct evolution of the original `guid-typescript` project, distinguished by its strong emphasis on type safety and a TypeScript-first API design, ensuring seamless integration into modern TypeScript codebases. The project's release cadence is primarily driven by bug fixes and dependency updates, indicating a stable and mature codebase.","status":"active","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/avmaisak/typescript-guid","tags":["javascript","typescript","guid","identifier"],"install":[{"cmd":"npm install typescript-guid","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-guid","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-guid","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses ES module syntax. While CommonJS might work in some transpiled setups, `import` is the recommended and type-safe approach.","wrong":"const { Guid } = require('typescript-guid');","symbol":"Guid","correct":"import { Guid } from 'typescript-guid';"},{"note":"GUIDs are created via static factory methods like `Guid.create()` or `Guid.parse()`, not directly instantiating the class.","wrong":"const newGuid = new Guid();","symbol":"Guid.create","correct":"const newGuid = Guid.create();"},{"note":"`isGuid` provides robust validation for any type, not just `Guid` instances. `instanceof` only checks the prototype chain.","wrong":"if (myValue instanceof Guid) { /* ... */ }","symbol":"Guid.isGuid","correct":"if (Guid.isGuid(myValue)) { /* ... */ }"}],"quickstart":{"code":"import { Guid } from \"typescript-guid\";\n\n// Example class demonstrating basic Guid usage\nexport class UserProfile {\n    public readonly id: Guid;\n    public name: string;\n\n    constructor(name: string, existingId?: string) {\n        this.id = existingId ? Guid.parse(existingId) : Guid.create();\n        this.name = name;\n    }\n\n    public equals(otherProfile: UserProfile): boolean {\n        return this.id.equals(otherProfile.id);\n    }\n\n    public toString(): string {\n        return `User: ${this.name}, ID: ${this.id.toString()}`;\n    }\n}\n\n// Usage example:\nconst user1 = new UserProfile('Alice');\nconst user2 = new UserProfile('Bob', user1.id.toString()); // Bob gets Alice's ID\nconst user3 = new UserProfile('Charlie');\n\nconsole.log(user1.toString());\nconsole.log(user2.toString());\nconsole.log(user3.toString());\nconsole.log(`Are user1 and user2 the same? ${user1.equals(user2)}`); // Should be true\nconsole.log(`Are user1 and user3 the same? ${user1.equals(user3)}`); // Should be false\n\n// Check if a string is a valid Guid\nconst potentialGuid = \"b77d409a-10cd-4a47-8e94-b0cd0ab50aa1\";\nconsole.log(`'${potentialGuid}' is a Guid: ${Guid.isGuid(potentialGuid)}`);","lang":"typescript","description":"This quickstart demonstrates creating new GUIDs, parsing existing ones, comparing `Guid` instances, and validating GUID strings. It showcases the primary API methods for common UUID operations."},"warnings":[{"fix":"Review any code that compares `Guid` objects using the `equals` method. If case-sensitivity was a requirement, you may need to implement custom string comparison after converting GUIDs to string format.","message":"The `equals` method, used for comparing two `Guid` instances, became case-insensitive starting from version 1.0.1. If your application previously relied on case-sensitive GUID comparisons for any reason, this change might alter behavior.","severity":"gotcha","affected_versions":">=1.0.1"},{"fix":"Always use `import { Guid } from 'typescript-guid';` for importing the library. Ensure your project is configured to handle ES modules (e.g., via `\"type\": \"module\"` in `package.json` for Node.js, or a bundler like Webpack/Rollup for browser applications).","message":"The library primarily targets modern JavaScript environments and ships with ES module (ESM) syntax. Using `require()` for imports in Node.js projects configured for ESM, or in browser environments without proper transpilation, can lead to runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use static factory methods like `Guid.create()` to generate a new GUID, or `Guid.parse(guidString)` to create one from a string.","cause":"Attempting to create a `Guid` instance using `new Guid()` instead of a static factory method.","error":"TypeError: Guid is not a constructor"},{"fix":"Verify that your Node.js project or build setup correctly handles ES Modules. For Node.js, ensure `\"type\": \"module\"` is set in your `package.json` or use a transpiler like Babel/TypeScript to convert ESM to CommonJS if strictly necessary for an older runtime.","cause":"This error often occurs when trying to `import { Guid } from 'typescript-guid'` in a CommonJS-only environment where the package might be transpiled or bundled incorrectly, or if the `package.json` entry points are misconfigured for your runtime.","error":"SyntaxError: Named export 'Guid' not found. The requested module 'typescript-guid' does not provide an export named 'Guid'"}],"ecosystem":"npm"}