cannot emit single object with typescript
Support plan
- is this issue currently blocking your project? (yes/no): no (but we need to use
any) - is this issue affecting a production system? (yes/no): no
Context
-
node version: 18
-
module version with issue: 5.0.0
-
last module version without issue: 4.1.3
-
environment (e.g. node, browser, native): nodejs
-
used with (e.g. hapi application, another framework, standalone, ...): hapi
-
typescript: 4.9.4
What are you trying to achieve or the steps to reproduce?
import * as Podium from '@hapi/podium';
interface ChangeEvent {
table: string;
change: string;
}
interface CustomPodiumEvents {
change: (data: ChangeEvent) => void;
}
const podium = new Podium.Podium<CustomPodiumEvents>({
name: 'change',
spread: false,
});
// nor
// podium.registerEvent({ name: 'change', spread: false });
podium.on<ChangeEvent>({ name: 'change' }, (event) => {
console.log(event.table);
});
podium.emit('change', { table:'foo', change: 'bar' });
What was the result you got?
compiling errors
> tsc asd.ts
asd.ts:20:11 - error TS2344: Type 'ChangeEvent' does not satisfy the constraint 'any[]'.
Type 'ChangeEvent' is missing the following properties from type 'any[]': length, pop, push, concat, and 29 more.
20 podium.on<ChangeEvent>({ name: 'change' }, (event) => {
~~~~~~~~~~~
asd.ts:24:25 - error TS2345: Argument of type '{ table: string; change: string; }' is not assignable to parameter of type '[data: ChangeEvent]'.
Object literal may only specify known properties, and 'table' does not exist in type '[data: ChangeEvent]'.
24 podium.emit('change', { table:'foo', change: 'bar' });
~~~~~~~~~~~
Found 2 errors in the same file, starting at: asd.ts:20
What result did you expect?
I should be able to emit one event (not an array) and manage one item in the listener.
To let it works, here is a workaround: note the ChangeEvent[] and the [{ table: 'foo'...}] (as an array) but the types in the listener is not an array, but a ChangeEvent
podium.on<ChangeEvent[]>({ name: 'change' }, (event) => {
console.log(event.table); // ! wrong type, it is an array
});
podium.emit('change', [{ table: 'foo', change: 'bar' }]);
As it is, the .on() generics are not intended to be explicitly defined, only inferred. How does it work without defining it? Ie. podium.on(…).
As for .emit(), it seems that the typings incorrectly assume that spread is always applied to the event when custom events are supplied, thus expecting an array for the second argument. I'm not sure there is a reasonable way to fix this given the stateful nature of the option. Maybe just accept both variants for now?
How does it work without defining it?
The listener's input definition doesn't change (still a single object) and the .emit wants an array too
Maybe just accept both variants for now?
I think it should be doable with function overloading https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads
I saw an example here: https://github.com/fastify/fastify-multipart/blob/b5591b6890f2e6adb4896b9db601246c20ae282d/index.d.ts#L176