Giter VIP home page Giter VIP logo

extending-event-emitter-with-flow's Introduction

Extending Event Emitter with Flow Types

This repo serves as an answer to a stackoverflow question on how to extend EventEmitter and still let Flow know what kind of arguments were passed to the event emitter.

Original question:

As an example, let's say I have a class that only emits three possible events – 'pending' or 'success' or 'failure'. Additionally, the type of the argument received in the eventHandler depends on which event was emitted –

  • if 'pending', the eventHandler receives no argument
  • if 'success', the eventHandler receives a number
  • if 'failure', the eventHandler receives an Error

Here is how I tried to model that:

// @flow

import EventEmitter from 'events'

type CustomEventObj = {| pending: void, success: number, error: Error |}

declare class MyEventEmitter extends EventEmitter { on<K: $Keys>( eventName: K, eventHandler: ( e: $ElementType<CustomEventObj, K>, ...args: Array ) => void ): this }

However, this results in an error like so:

Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ test.js:12:3

Cannot extend EventEmitter [1] with MyEventEmitter because an indexer property is missing in CustomEventObj [2] in the first argument of property on.

[1]  3│ import EventEmitter from 'events'
      :
     8│   error: Error
     9│ |}
    10│
    11│ declare class MyEventEmitter extends EventEmitter {
[2] 12│   on<K: $Keys<CustomEventObj>>(
    13│     eventName: K,
    14│     eventHandler: (
    15│       e: $ElementType<CustomEventObj, K>,
    16│       ...args: Array<any>
    17│     ) => void
    18│   ): this
    19│ }
    20│

I don't want to have an indexer property on CustomEventObj because wouldn't that kill the point of only having 3 possible events?

Any help would be appreciated.

extending-event-emitter-with-flow's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.