From 3efbc264bae1b93b812d72942a6942221fed77a5 Mon Sep 17 00:00:00 2001 From: Lucille Hua Date: Tue, 4 Feb 2025 12:55:31 -0500 Subject: [PATCH] [typeid-js] improve type inference for typeid.toString (#429) ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 license](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request I represent that I have the right to license the contributions to the project maintainers under the Apache 2 license. ## Summary Addresses https://github.com/jetify-com/typeid-js/issues/15. The caller is now able to get the exact type based on typeid prefix, since the return type is conditionally determined by `T extends ""` cc: @programmarchy ## How was it tested? devbox run test --- typeid/typeid-js/package.json | 2 +- typeid/typeid-js/src/typeid.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/typeid/typeid-js/package.json b/typeid/typeid-js/package.json index 1cac2de..b950361 100644 --- a/typeid/typeid-js/package.json +++ b/typeid/typeid-js/package.json @@ -1,6 +1,6 @@ { "name": "typeid-js", - "version": "1.1.0", + "version": "1.2.0", "description": "Official implementation of the TypeID specification in TypeScript. TypeIDs are type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs", "keywords": [ "typeid", diff --git a/typeid/typeid-js/src/typeid.ts b/typeid/typeid-js/src/typeid.ts index df9e796..599a30e 100644 --- a/typeid/typeid-js/src/typeid.ts +++ b/typeid/typeid-js/src/typeid.ts @@ -41,11 +41,11 @@ export class TypeID { return stringify(this.toUUIDBytes()); } - public toString(): `${T}_${string}` | string { + public toString(): T extends "" ? string : `${T}_${string}` { if (this.prefix === "") { - return this.suffix; + return this.suffix as T extends "" ? string : `${T}_${string}`; } - return `${this.prefix}_${this.suffix}`; + return `${this.prefix}_${this.suffix}` as T extends "" ? string : `${T}_${string}`; } static fromString(