dmonad / lib0

Monorepo of isomorphic utility functions
MIT License
368 stars 65 forks source link

ObservableV2 does not accept an interface as its generic parameter #76

Closed somebody1234 closed 1 year ago

somebody1234 commented 1 year ago

Describe the bug ObservableV2 does not accept A as a generic parameter if A is not a @typedef. Related to:

To Reproduce

import { ObservableV2 } from 'lib0/observable';

class A {
  /** @type {() => void} */ a;
}

/** @type {ObservableV2<A>} */
const what = /** @type {any} */ ({});

See this TypeScript playground link. (Note that the implementation of lib0/observable has been copied into the link - the TS playground seems to have problems importing its type definitons from npm)

Expected behavior No type errors

See this TypeScript playground link for a demo of a potential solution.

Screenshots N/A

Environment Information

Additional context this is a potential solution:

/**
 * @template {{[key in keyof EVENTS]: function(...any):void}} EVENTS
 */

it is implemented in the link in the "expected behavior" section above

it isn't valid JSDoc syntax though, so it may not be an option if you're planning on generating docs from the source.

dmonad commented 1 year ago

Okay, we can implement it like that. See my last commit. I don't know why this would be useful. Also, I'm not sure if a self-referencing template is a good idea or not, just because it is still working. This also allows the use of symbols and numbers as event names (which is not necessarily a good thing).