Adding type safety to object IDs in TypeScript
11–20 of 61 posts
Re: Adding type safety to object IDs in TypeScript
#12This is pretty close to type branding (newtype wrapping for the Haskell-inclined), though using template literal types is pretty novel. Normal brands look something like this: type Brand = BaseType & { readonly __brand__: Brand }; type FooId = Brand ; function fooBar(asdf: FooId | 'foobar'): void { } fooBar will only accept the literal string 'foobar' or a true FooId, but not any arbitrary string. FooId would then co…
declare const isMyID: unique symbol;
export type MyID = string & { [isMyID]: true };Re: Adding type safety to object IDs in TypeScript
#13I wonder if a similar (but maybe more bloated) implementation using interfaces (and probably generics too?) will work in this case.
https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgIlAvFA...
Alternatively, with generics: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgA...
I don't think these are better, to be honest. The string types suffice and are easier to exchange with servers and other APIs.
Re: Adding type safety to object IDs in TypeScript
#14This is pretty close to type branding (newtype wrapping for the Haskell-inclined), though using template literal types is pretty novel. Normal brands look something like this: type Brand = BaseType & { readonly __brand__: Brand }; type FooId = Brand ; function fooBar(asdf: FooId | 'foobar'): void { } fooBar will only accept the literal string 'foobar' or a true FooId, but not any arbitrary string. FooId would then co…
Re: Adding type safety to object IDs in TypeScript
#15This is pretty close to type branding (newtype wrapping for the Haskell-inclined), though using template literal types is pretty novel. Normal brands look something like this: type Brand = BaseType & { readonly __brand__: Brand }; type FooId = Brand ; function fooBar(asdf: FooId | 'foobar'): void { } fooBar will only accept the literal string 'foobar' or a true FooId, but not any arbitrary string. FooId would then co…
I prefer the Brand solution, it works well for existing ID sets that you can't easily migrate to have an actual string prefix
Re: Adding type safety to object IDs in TypeScript
#16Re: Adding type safety to object IDs in TypeScript
#17Its bit sad that startsWith doesn't narrow the type, making this pattern slightly less convenient. The GH issue: https://github.com/microsoft/TypeScript/issues/46958
Re: Adding type safety to object IDs in TypeScript
#18Re: Adding type safety to object IDs in TypeScript
#19The proliferation of string identifiers is a pet peeve of mine. It’s what I call “stringly typed” code (not my coinage but I use it all the time).
Re: Adding type safety to object IDs in TypeScript
#20Not sure if it’s a valid point, but what I would like to have - kind of a regex or a template for strings or numbers. Otherwise, it’s still just a sting or a specific value. It’s not like you are free to update backend to prefix ids to your liking. Most of the time you have to work with set schemas.