-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
Allow multiple brands and add Unbranded utility #88
Conversation
✅ Deploy Preview for valibot ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
library/src/methods/brand/brand.ts
Outdated
@@ -633,6 +672,20 @@ export function brand< | |||
/** | |||
* Brands the output type of a schema. | |||
* | |||
* @example |
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.
To keep the source code as simple as possible, I prefer not to add examples. I will add examples in the future to the API reference on the website.
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.
I'll delete it
expectTypeOf<BrandedPostId>().not.toMatchTypeOf<BrandedUserId>(); | ||
}); | ||
|
||
test('should support unbranding', () => { |
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.
Why is unbranding and this test necessary? Is there a practical use case for this?
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.
Yes, It's for practical use case.
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.
Can you explain the practical use case? Does Zod have this feature?
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.
I was using it to create a mapper to convert database unbranded types from ORM to branded types without schema parsing (because it was already validated on insert step) with type safety (database row should match unbranded schema). Like imagine you have a large object schema with branded fields and you don't want to manually create an object type with the same field's types but unbranded. So you could write typescript generic that unwraps all fields with Unbranded
utility. Actually, it's not required, but convinient for a user. The user may write Unbranded
by themselves, because I exported BrandSymbol
, so if you will decide to remove it, it can take a good place in the method's docs hints.
library/src/methods/brand/brand.ts
Outdated
* @example | ||
* Unbranded<string & Brand<'foo'> & Brand<'bar'>> = string | ||
*/ | ||
export type Unbranded<BrandedType> = BrandedType extends infer T & |
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.
If the newly added types are only needed in the tests, they should not be in the brand.ts
file, but directly under brand.test.ts
. Or alternatively under types.ts
in the brand
folder if there is a practical use case for it outside the library code.
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.
Yes, It's for outside code. Should I export the file types.ts
from index.ts
in the folder?
This reverts commit 246bb43.
I will review the PR next week. |
I have now checked your code. We can extend the |
I see. Anyway, please, stay exporting brand symbol for possibility of implementing these types by user themselves. I will edit this pr later, if you don't do this sooner. |
fixes #87
I found
UnionToIntersection
type in keyof.ts and decided to just copy it in brand.ts. Should it imports type for code deduplication?