Replies: 7 comments 13 replies
-
This is definitely interesting from a technical perspective but I'm not sure what problem we'd be solving here. What's the high level issue you're trying to solve? I think most calls to the API here would be through GraphQL, and eventually REST APIs and the id received would be a base64-encoded string or the uuid. In those cases, we'd need to work around all this and not get any of the benefits here. How often do you have the wrong id being passed to a different API? |
Beta Was this translation helpful? Give feedback.
-
FWIW, I built something quite similar last year and I chose to have typed IDs which saved me and a couple of my colleagues from embarrassing bugs more than a few times since. I went with branded types like @lazytype suggests and while there was some overhead I feel like it was overall worth doing. I guess the only difference is that we made the node type a string export default class EntUserSchema extends EntSchema {
config: {
type: EntIDTypes.USER,
// ...
}
// ...
}
// ---
enum EntIDTypes {
USER = "user",
}
type EntID<T extends EntIDTypes> = string & { __brand: T } // all IDs are UUIDs
type EntUserID = EntID<EntIDTypes.USER>; |
Beta Was this translation helpful? Give feedback.
-
what are the proposals/thoughts for how to integrate with non-ent code? I'm currently working with an existing codebase and one of the issues I've run into is converting ent ids into strings (what the old API supported) and what I've been doing is: function (id: string) {}
const ent: Ent = getEnt();
// toString() is annoying
foo(ent.id.toString()); how does this work with this proposal? |
Beta Was this translation helpful? Give feedback.
-
Right now, we encode ids in graphql using base 64 encoding the id would have the type and underlying id so that could be simplified, what should we send down here? when base64 ids are disabled, we'd presumably just send the uuid? |
Beta Was this translation helpful? Give feedback.
-
https://twitter.com/_bwhitty/status/1293765976840839168 one of the issues I'm concerned about is the complexity here. this just seems more complicated... |
Beta Was this translation helpful? Give feedback.
-
ok, i'm down to try and sketch it out and see what it'll look like. I don't really have a contributing.md or have a document that lays out the lay of the land. if you wanna give it a go, how do you want to start? I also have a simple CLA I'd like you to sign if you want to contribute code. you can send me an email a [email protected] and I can send it to you. I need to automate this eventually but not there yet. |
Beta Was this translation helpful? Give feedback.
-
I created #611 to track |
Beta Was this translation helpful? Give feedback.
-
It would be great to leverage TypeScript to make things like the following an error:
(this is similar to the UID/OID distinction in
www
)TL;DR: Go here for a working demonstration.
This would be possible using a somewhat well-known nominal typing trick called "branded" types. The TypeScript handbook demonstrates a few ways to implement this [0], and there is the more recent method using symbols [1].
At a high level, the approaches fall into two buckets:
value
and for users to treat the ID type as opaque.string
/number
:Theoretically, this could also make the internals better-typed as well. The following generated code:
might be able to be re-written as this:
Practically speaking, to make a change like this happen we'd need to make the
Ent
interface generic with respect to thenodeType
and work around TS's inability to express "Self" in a generic class, e.g.It's not the most attractive, but it'd all be generated code that's hidden from the user.
Altogether, here's a working demonstration!. Please let me know your thoughts and I'd be happy to attempt a pull request 🙂.
[0] https://basarat.gitbook.io/typescript/main-1/nominaltyping
[1] https://twitter.com/rauschma/status/1293752309248397315
Beta Was this translation helpful? Give feedback.
All reactions