Skip to content

Commit

Permalink
[typeid-js] improve type inference for typeid.toString (#429)
Browse files Browse the repository at this point in the history
## 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 jetify-com/typeid-js#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
  • Loading branch information
LucilleH authored Feb 4, 2025
1 parent 4395557 commit 3efbc26
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion typeid/typeid-js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions typeid/typeid-js/src/typeid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export class TypeID<const T extends string> {
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<const T extends string>(
Expand Down

0 comments on commit 3efbc26

Please sign in to comment.