[TypeScript] Type error when passing SetupContext to a composable function which expects a partial of the EmitsOptions
Version
3.0.5
Reproduction link
Steps to reproduce
The type error is visible in the TS Playground.
What is expected?
No type error
What is actually happening?
I get the type error
Argument of type 'SetupContext<{ a: null; load: null; }>' is not assignable to parameter of type 'SetupContext<{ load: any; }>'.
Property 'a' is missing in type '{ load: any; }' but required in type '{ a: null; load: null; }'
We want to create a type-safe composable function which emits an event. The function can be used by different components which emits different events, i.e. their EmitsOptions are not equal. The composable function should be able to specify only the specific event which it expects the EmitsOptions to contain (i.e. a partial of the components' EmitsOptions), but this results in a type error.
The problem is due to the type of EmitFn (not exactly sure why), but if I change the type to something simple like
type EmitFn<Options = ObjectEmitsOptions> = (ev: keyof Options, ...args: any[]);
then it works (see the reproduction link).
The only work-around I see is to have the composable take SetupContext<any>
as argument, but then type-safety is lost, i.e. the components could forget to add the emitted event of the composable function.
I believe that will be fixed in https://github.com/vuejs/vue-next/pull/2164