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

Using this in a TypeScript library with "declaration": true fails #40

Closed
ifiokjr opened this issue Mar 1, 2020 · 1 comment · Fixed by #41
Closed

Using this in a TypeScript library with "declaration": true fails #40

ifiokjr opened this issue Mar 1, 2020 · 1 comment · Fixed by #41

Comments

@ifiokjr
Copy link
Contributor

ifiokjr commented Mar 1, 2020

Description

The error is shown below.

Public property 'events' of exported class has or is using name 'Emitter' from external module "/path/to/node_modules/nanoevents/index" but cannot be named

The fix would be to update the index.d.ts file for the project to look like this. This works because when setting export = createNanoEvents then we also have to wrap everything in a namespace so that it can be accessed by external code.

declare namespace createNanoEvents {
  interface EventsMap {
    [event: string]: any
  }

  interface DefaultEvents extends EventsMap {
    [event: string]: (...args: any) => void
  }

  class Emitter<Events extends EventsMap> {
    /**
     * Event names in keys and arrays with listeners in values.
     *
     * ```js
     * emitter1.events = emitter2.events
     * emitter2.events = { }
     * ```
     */
    events: Partial<{ [E in keyof Events]: Events[E][] }>

    /**
     * Add a listener for a given event.
     *
     * ```js
     * const unbind = ee.on('tick', (tickType, tickDuration) => {
     *   count += 1
     * })
     *
     * disable () {
     *   unbind()
     * }
     * ```
     *
     * @param event The event name.
     * @param cb The listener function.
     * @returns Unbind listener from event.
     */
    on <K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void

    /**
     * Calls each of the listeners registered for a given event.
     *
     * ```js
     * ee.emit('tick', tickType, tickDuration)
     * ```
     *
     * @param event The event name.
     * @param args The arguments for listeners.
     */
    emit <K extends keyof Events>(
      this: this,
      event: K,
      ...args: Parameters<Events[K]>
    ): void
  }
}

/**
 * Create event emitter.
 *
 * ```js
 * import createNanoEvents from 'nanoevents'
 *
 * class Ticker {
 *   constructor() {
 *     this.emitter = createNanoEvents()
 *   }
 *   on(...args) {
 *     return this.emitter.on(...args)
 *   }
 *   tick() {
 *     this.emitter.emit('tick')
 *   }
 * }
 * ```
 */
declare function createNanoEvents<Events extends createNanoEvents.EventsMap = createNanoEvents.DefaultEvents> (
): createNanoEvents.Emitter<Events>

export = createNanoEvents
@johnnyshankman
Copy link

Is this actually fixed? I'm having this issue in 7.0.0 but not 6.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants