feat: add template literal types for formatted ids#594
feat: add template literal types for formatted ids#594andrewpeters9 wants to merge 4 commits intozenstackhq:devfrom
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| import type { | ||
| ExtractIdFormat, | ||
| FormatToTemplateLiteral, | ||
| MapStringWithFormat, | ||
| } from '../src/utils/type-utils'; | ||
|
|
||
| describe('FormatToTemplateLiteral', () => { | ||
| it('converts simple prefix format to template literal', () => { | ||
| expectTypeOf<FormatToTemplateLiteral<'user_%s'>>().toEqualTypeOf<`user_${string}`>(); | ||
| }); |
There was a problem hiding this comment.
This file is a bit verbose - let me know if you'd prefer me to trim it down
| //#region Field defaults | ||
|
|
||
| /** | ||
| * ID generator functions that support format strings (e.g., uuid(4, "user_%s")) | ||
| */ | ||
| export const ID_GENERATOR_FUNCTIONS = ['uuid', 'cuid', 'nanoid', 'ulid'] as const; | ||
| export type IdGeneratorFunction = (typeof ID_GENERATOR_FUNCTIONS)[number]; |
There was a problem hiding this comment.
Couldn't find a more appropriate file for this (but admittedly, seems very misplaced 😅)
Advise if you have another file in mind / or would prefer a new file
6aea088 to
54f8656
Compare
|
This would be a very cool feature. I hope everything works out in the end, because I do agree that this is a big improvement for safety. And I'm happy to see more improvements for ID formatting so quickly after its release :D |
Thanks for the feedback :) Given you implemented this feature: do you think it should default to the narrower type of |
|
@andrewpeters9 we are in a bit of a pickle at the moment because the format is not actually enforced at runtime. There's nothing stopping the user from inputting whatever ID they wanted, so the most appropriate type for the ID is indeed If an ID field is readonly and has some inferable default value, then I think it should be narrowed. That makes the most sense to me because it maps 1-1 with |
|
@sanny-io Pardon late reply. Before reading this I realised the same thing while thinking about the MR over the last week. Some other thoughts:
E.g. a rough example And then this would be internally handled by either zod validation or whichever way emails were validated when a user would do At the same time, this doesn't exactly provide us a clear pattern, as the addition of |
|
@andrewpeters9 good points you bring up. A That feature might be of interest for an implementation of template literals. |
Marking this as a draft, as it still requires more work & I was wanting input from the maintainers.
Idea:
Allow for the TS types of the newly (optionally) formatted IDs to narrow to
prefix_{string}as opposed to juststring:Motivations:
Concerns:
User["id"]with the assumption that it casts to astringnarrowTypeor something (defaults tofalse)