-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
fix(createEventHook): type check for multiple arguments #4555
fix(createEventHook): type check for multiple arguments #4555
Conversation
Should we care about the |
looks like an typecheck issue (https://app.netlify.com/sites/vueuse/deploys/67a08230f106910008a97622). So we should care. |
the test that fails is this one it('should pass union type', () => {
let count = 0
const { on: onResult, trigger } = createEventHook<number | string>()
// union type should be inferred
onResult(value => count = 2)
trigger(1)
trigger(2)
expect(count).toBe(2)
}) Adding makes the type more complex so in order to interfere union types it should be declared we should find a common ground for this case |
- Undo previous changes for union types - Fix type inference for union types in Callback<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.
i wonder if we should simplify slightly and reduce our use of something like this playground type Callback<T> = IsAny<T> extends true
? (...param: unknown[]) => void
: (
[T] extends [void]
? () => void
: T extends unknown[]
? (...param: T) => void
: (param: T) => void
) its a weird type at the minute in
you can merge this PR as it currently stands, don't count me as a blocker. but you should probably at least change does anyone know why we test against |
For me, I would go with the simplest and cleanest solution: with this one typing arguments is easy and clean you get what you expect and you are not limited example: TsPlayground |
yes but the conditional types are there to allow it'd be a breaking change to the types if we drop those Callback<any>;
// actually represents (...params: any[]) => void
// DOES NOT represent (param: any) => void
Callback<void>;
// actually represents (...params: unknown[]) => void
// DOES NOT represent (param: void) => void for whatever reason, we have these two special cases. and that's why the type looks so wonky |
that's why I added the change as an extra condition not to have breaking changes but if I change
it('should pass union type', () => {
let count = 0
const { on: onResult, trigger } = createEventHook<number | string>()
// union type should be inferred
onResult(value => count = 2)
trigger(1)
trigger(2)
expect(count).toBe(2)
}) |
That is a trick to tackle the way the conditional type expressions work. Other than that, I do agree that adding the additional |
aha yeah, ok makes sense! otherwise it just applies to each member of the union, so would be something like
to be honest i'd love to see these three weird cases vanish in the next major version Callback<never>; // (...params: unknown[]) => void
Callback<any>; // (...params: any) => void
Callback<void>; // (..params: unknown[]) => void i actually don't think it is intuitive that any of these resolve to i would've expected this: Callback<never>; // (param: never) => void
Callback<any>; // (param: any) => void
Callback<void>; // (param: void) => void then i'd ask the consumer what they're trying to do, as the the i've taken this on a bit of a tangent so maybe we should discuss outside of this PR. but i do feel like this type is a bit of a mess right now |
maybe we can fine tune this, to solve the problem with v12 and do a follow up with the breaking changes for v13? |
i think so i think this PR is all good and we can discuss other changes for the next major |
Before submitting the PR, please make sure you do the following
fixes #123
).Description
This PR improves the typing of
createEventHook
to correctly support multiple arguments when used with tuple types. Previously, tuple types were treated as a single argument instead of separate parameters.Before (Issue)
After (Fix)
Additional context
-The fix introduces an extra condition:
This ensures that tuples are spread as separate arguments instead of being treated as a single parameter.