cannot emit single object with typescript
- Dominant language
- JavaScript
- Stars
- 86
- Forks
- 22
- PR merge metrics
- No merged PRs in 30d
Description
#### 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?
```js
import * as Podium from '@hapi/podium';
interface ChangeEvent {
table: string;
change: string;
}
interface CustomPodiumEvents {
change: (data: ChangeEvent) => void;
}
const podium = new Podium.Podium({
name: 'change',
spread: false,
});
// nor
// podium.registerEvent({ name: 'change', spread: false });
podium.on({ 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({ 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`
```js
podium.on({ name: 'change' }, (event) => {
console.log(event.table); // ! wrong type, it is an array
});
podium.emit('change', [{ table: 'foo', change: 'bar' }]);
```
Contributor guide
Assessment
This issue has not been assessed yet.