-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add template literal types for formatted ids #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
feat: add template literal types for formatted ids #594
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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)