dmonad / lib0

Monorepo of isomorphic utility functions
MIT License
345 stars 63 forks source link

ObservableV2 does not accept interfaces #72

Closed somebody1234 closed 10 months ago

somebody1234 commented 11 months ago
class ObservableV2<EVENTS extends {
    [key: string]: (...arg0: any[]) => void;
}> {}

rejects interfaces because interfaces don't have an index signature. would it be possible to do this instead?

class ObservableV2<EVENTS extends {
    [key in EVENT_TYPES]: (...arg0: any[]) => void;
}, EVENT_TYPES extends keyof EVENTS = keyof EVENTS> {}
somebody1234 commented 11 months ago

Also NAME extends string should probably be NAME extends keyof EVENTS (or EVENT_TYPES) so that autocomplete actually works (?)

Frizi commented 11 months ago

I believe that the EVENT_TYPES generic could also be avoided.

class ObservableV2<EVENTS extends {
    [key in keyof EVENTS]: (...args: any[]) => void;
}> {

  on<NAME extends keyof EVENTS>(name: NAME, f: EVENTS[NAME]) {
   // ...
  }
}

This variant seems to support interfaces just fine, and autocomplete works too.

dmonad commented 10 months ago

Sometimes I don't know if I should take an issue seriously or not. You didn't bother to give any details or hints to the issue you are describing. I can't find EVENT_TYPEs. I'm not using typescript.

Please help me save some time by opening a new ticket and filling out the template that I carefully provided.

dmonad commented 10 months ago

I looked through the codebase and I think I found what you mean. Note: ObservableV2 is still not done (should be marked as experimental). I will mark it stable once I use it myself in one of my projects..