Skip to content
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

cannot emit single object with typescript #81

Open
Eomm opened this issue Jan 19, 2023 · 2 comments
Open

cannot emit single object with typescript #81

Eomm opened this issue Jan 19, 2023 · 2 comments
Labels
bug Bug or defect support Questions, discussions, and general support types TypeScript type definitions

Comments

@Eomm
Copy link

Eomm commented Jan 19, 2023

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

image

> 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' }]);
@Eomm Eomm added the support Questions, discussions, and general support label Jan 19, 2023
@kanongil
Copy link
Contributor

kanongil commented Jan 19, 2023

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?

@kanongil kanongil added types TypeScript type definitions bug Bug or defect labels Jan 19, 2023
@Eomm
Copy link
Author

Eomm commented Jan 20, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect support Questions, discussions, and general support types TypeScript type definitions
Projects
None yet
Development

No branches or pull requests

2 participants