jetify-com / typeid-js Goto Github PK
View Code? Open in Web Editor NEWTypeScript implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
License: Apache License 2.0
TypeScript implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
License: Apache License 2.0
Using this tool: https://cryptii.com/pipes/crockford-base32
But the tests for the same input shows:
I was porting the code to Elixir using https://hex.pm/packages/crockford_base32 and the result from the library matches the online tool but does not match the Typescript implementation.
test "encode" do
assert Typeid.encode("user", "01889c89-df6b-7f1c-a388-91396ec314bc") == {
:ok,
"user_0649s2ezddzhs8w8j4wpxgrmqg"
}
end
Any plans to make TypeIDs even safer at the type level using nominal typing ?
It would be great to have an API that fails typechecking for instances like these :
function getUser(userId: TypeID<'user'>) { ... }
const userId = typeid('user') // Inferred as TypeID<'user'>
const postId = typeid('post') // Inferred as TypeID<'post'>
getUser(userId) // Ok
getUser(postId) // Fails at compile time
New parsing functions could be added to instanciate properly typed TypeIDs :
// Inferred as TypeId<'prefix'
const prefixId = TypeID.fromPrefixedString('prefix', 'prefix_00041061050r3gg28a1c60t3gf')
// Inferred as TypeId<'prefix'> but fails at runtime.
const otherPrefixId = TypeID.fromPrefixedString('prefix', 'other_00041061050r3gg28a1c60t3gf')
Typescript, since 4.1, allows for string templates such as \
user_${string}``. I've taken advantage of this to create "string types" to pair with my TypeID types:
export type UserTypeString = `user_${string}`;
export type UserTypeID = TypeID<'user'>;
// ...
private connectedUsers: Record<UserTypeString, User> = {};
You can't use UserTypeID
as an index, but you can use UserTypeString
, and it makes it a lot clearer exactly what you're keying by.
However, later, when you do something like this, it will fail:
const userId = user.id.toString();
connectedUsers[userId] = user;
toString()
returns a string
, but I want it to return a user_${string}
. For now, I'm planning on subclassing TypeID in my project and overriding the toString()
function to work the way I want, but is this something there would be interest in having added to the base library?
Specifically, I've added:
public toTypeString(): `${T}_${string}` {
return this.toString() as `${T}_${string}`;
}
Instead of modifying toString()
, since that'll break everything that is expecting a regular string
from that function. This seems like a pretty simple little addition that gives a fair extra bit of clarity when using the string form of TypeIDs as indexes.
Is there a reason for this behavior?
Hi, first of all thanks for this project! I liked using it for my project but had some trouble because of the in-memory representation of TypeIDs using class instances.
One issue was that I needed to serialize and de-serialize it using a custom type in Drizzle ORM:
const makeCustomId = <T extends string>(dbName: string, prefix: T) =>
customType<{
data: TypeID<T>;
driverData: string;
}>({
dataType() {
return "string";
},
fromDriver(value: string): TypeID<T> {
return TypeID.fromString(value).asType(prefix);
},
})(dbName);
This works, apart from seeing [Object object]
in some devtools.
Another issue was returning TypeID values from a Qwik City routeLoader$
. It gives an error that it can't serialize these values as it only supports serializing JS primitives, arrays and plain objects. This meant I had to map every value I get from db first to update the IDs to string, and then back to TypeID's on the client side.
To make my workflow easier, I forked typeid-js and built an alternative that always represents TypeIDs as strings at runtime, provides some helper functions, and uses branded TypeScript types for type safety: https://github.com/ozanmakes/typeid-unboxed
I'm curious to hear what you think. Is this something you'd like to include in this package? Otherwise, do you bless such an approach and would you mind if I publish this on npm under this name or something else?
I saw there's two implementations of TypeID linked:
Is there any guidance on which one to use, and why you might choose one over the other?
My company is stuck on TS 4.9.4, and out of the box this library is broken.
Is there a minimum TS version that should be documented in the README.md
? I see typescript
is at some 5.x
version in the package.json
.
Build errors:
../../../node_modules/typeid-js/dist/typeid.d.ts:1:22 - error TS1139: Type parameter declaration expected.
1 declare class TypeID<const T extends string> {
~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:1:30 - error TS1005: ',' expected.
1 declare class TypeID<const T extends string> {
~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:1:44 - error TS1005: ',' expected.
1 declare class TypeID<const T extends string> {
~
../../../node_modules/typeid-js/dist/typeid.d.ts:2:19 - error TS1005: ',' expected.
2 private prefix;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:3:19 - error TS1005: ',' expected.
3 private suffix;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:5:5 - error TS1005: ',' expected.
5 getType(): T;
~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:6:5 - error TS1005: ',' expected.
6 getSuffix(): string;
~~~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:5 - error TS1005: ',' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:12 - error TS1139: Type parameter declaration expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:18 - error TS1005: ':' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:20 - error TS1005: ',' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:28 - error TS1005: ':' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:42 - error TS1005: ')' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:45 - error TS1005: ',' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:46 - error TS1136: Property assignment expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:7:57 - error TS1005: '(' expected.
7 asType<const U extends string>(prefix: U): TypeID<U>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:8:5 - error TS1005: ',' expected.
8 toUUIDBytes(): Uint8Array;
~~~~~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:9:5 - error TS1005: ',' expected.
9 toUUID(): string;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:10:5 - error TS1005: ',' expected.
10 toString(): `${T}_${string}` | string;
~~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:5 - error TS1005: ',' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:23 - error TS1139: Type parameter declaration expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:29 - error TS1005: ':' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:31 - error TS1005: ',' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:39 - error TS1005: ':' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:50 - error TS1005: ')' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:58 - error TS1005: ',' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:59 - error TS1136: Property assignment expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:11:70 - error TS1005: '(' expected.
11 static fromString<const T extends string>(str: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:5 - error TS1005: ',' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:26 - error TS1139: Type parameter declaration expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:32 - error TS1005: ':' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:34 - error TS1005: ',' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:42 - error TS1005: ':' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:56 - error TS1005: ')' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:78 - error TS1005: ',' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:79 - error TS1136: Property assignment expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:12:90 - error TS1005: '(' expected.
12 static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:5 - error TS1005: ',' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:21 - error TS1139: Type parameter declaration expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:27 - error TS1005: ':' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:29 - error TS1005: ',' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:37 - error TS1005: ':' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~~~~~~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:51 - error TS1005: ')' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:68 - error TS1005: ',' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:69 - error TS1136: Property assignment expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~
../../../node_modules/typeid-js/dist/typeid.d.ts:13:80 - error TS1005: '(' expected.
13 static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>;
~
Found 46 errors in the same file, starting at: ../../../node_modules/typeid-js/dist/typeid.d.ts:1
Will this get published to NPM, or do we need to fork it?
Tried this in my frontend project and surprised that is simply crashed with "Buffer is not defined". Looks like it's assuming node.js environment. Is there a plan to make this package work in the browser? Not being able to use on the frontend would severely limit the adoption.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.