-
Notifications
You must be signed in to change notification settings - Fork 35
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
oneOfType
and oneOf
support generics
#147
Comments
If you agree, I can provide a PR. see: https://github.com/IduxFE/idux/blob/main/packages/cdk/utils/src/props.ts#L118 |
Hi @danranVm, some work to implement type generics is undergoing in #136 and other PRs referenced there. The latest changes have been published in If you need a fix for user: oneOfType([String, object<User>()]) For genre: oneOf(['action', 'thriller'] as const) |
Hi @dwightjack , thanks for your answer, I know both of these schemes. They work well in most cases, but there are some things that are not friendly. E.g: // If the non-displayed declaration is null, the type of footer cannot be deduced correctly
footer: oneOfType<any[] | VNode | null>([array(), vNode])
// spelling errors 😭, of course, this is not a problem with `vue-types`, but I want to avoid this problem as much as possible.
genre: oneOf(['action', 'thriler'] as const) |
@danranVm As for I have created a branch named As you can see from the examples, I created the following scenarios: // 1
export const oneOfTuple = oneOf<1 | 2 | 'string'>([1, 'string'] as const).def(2)
// 2
export const oneOfTuple2 = oneOf<1 | 2 | 'string'>([1, 2, 'string']).def(1)
// 3
export const oneOfTuple3 = oneOf([1, 2, 'string'] as const).def(1) I couldn't fix scenario 1, where there should be an error because the array does not include Any idea? (also tagging @victorgarciaesgi because this discussion is related to #140) |
I will look into this when I have time @dwightjack no problem! 😄 |
@victorgarciaesgi thanks for your support. for now, I was asking your opinion on the feature, since we are working on these kind of features as well. I don't want you to feel pressed to work on it. |
After some research, I am not sure that there's a way to fix the issue in #147 (comment) (I guess that's the nature of the union type, it cannot force the presence of a value). For now, I have created a Draft PR with the related changes (#151). I am not yet fully positive about it: while it helps with typos in the validation array, it allows scenarios with runtime errors not detected by the type checker like: oneOf<1 | 2 | 'string'>([1, 'string'] as const).def(2) // <-- at runtime this fails |
Support for union types in Reference: |
Using generics can help us better constraint types, likes:
The text was updated successfully, but these errors were encountered: