1

Here's how I would reliably override a method in vanilla JS without caring about names or number of arguments, or the return value:

import EventEmitter from 'events'

// console.log event + arguments every time this emitter emits anything.
// Just an example.  
class LogEmitter extends EventEmitter {
  emit(...args) {
    console.log('emit', ...args)
    return super.emit(...args)
  }
}

But in TypeScript, I hear complaints:

import { EventEmitter } from 'events'

class LogEmitter extends EventEmitter {
  emit(...args) {                // Rest Parameter 'args' implicitly has an any[] type
    console.log('emit', ...args)
    return super.emit(...args)  // Expected at least 1 arguments, but got 0 or more.
  }
}

I'm not sure how to tell TypeScript that this is ok. I don't want to know anything about the signature of the method I'm overriding, just print whatever arguments you're passed. Means I don't have to update my signature if/when the superclass signature changes, and ideally I would have one way to type this that would work for all methods I override, with maybe an exception of telling it which method name to copy the signature from.

Something like:

The type of ...args should be whatever the Parameters are to super.emit

But my newbie attempt at this isn't valid syntax:

import { EventEmitter } from 'events'

class LogEmitter extends EventEmitter {
  emit(...args: Parameters<super.emit>) {  // 'super' can only be referenced in members of derived classes or object literal expressions.
    console.log('emit', ...args)
    return super.emit(...args)
  }
}

My workaround is to any the required first argument:

import { EventEmitter } from 'events'

class LogEmitter extends EventEmitter {
  emit(type: any, ...args: any[]) {
    console.log('emit', type, ...args)
    return super.emit(type, ...args)
  }
}

But I think this is more like patching symptoms, since now this class reports a less accurate signature. I feel like there's a better solution that gives the correct signature for emit automatically without me having to copy it from super.emit.

How can I type this perfectly without having to know anything about the signature of super.emit?

2 Answers 2

2

Unfortunately, Typescript does not (as of TS4.1) contextually type subclass members by the analogous members in super classes (or implemented interfaces). There are a bunch of similar GitHub issues, but I think the canonical one for this particular situation is microsoft/TypeScript#23911. For now, all you can do is "patch symptoms":

As a workaround, if you want to refer to the super class, you will need to do so explicitly by name (EventEmitter) and not by super, and you can use a lookup type to get the emit method:

class LogEmitter extends EventEmitter {
  emit(...args: Parameters<EventEmitter['emit']>) {
    console.log('emit', ...args)
    return super.emit(...args)
  }
}

Playground link to code

3
  • Great. This worked. Do you know if there's any difference between Parameters<EventEmitter['emit']> and Parameters<typeof EventEmitter.prototype.emit>, as given in this similar answer by @willis stackoverflow.com/a/66020642/62851?
    – timoxley
    Feb 3, 2021 at 17:39
  • I noticed that if I used a different EventEmitter library (eventemitter3), it'd report two different resulting signatures depending on which option I used: EventEmitter['emit'] gives emit(event: string | symbol, ...args: any[]) but typeof EventEmitter.prototype.emit gives something different: emit(event: any, ...args: any[])?? With node's EventEmitter, they were the same signature (first option). Maybe there's something wrong with eventemitter3's typings, but I'm curious why there's any difference at all?
    – timoxley
    Feb 3, 2021 at 17:43
  • 1
    There would not normally be a difference, assuming that EventEmitter.prototype has type EventEmitter (which it does in TypeScript, despite that not really being accurate), then typeof EventEmitter.prototype.emit should be the same type as EventEmitter['emit']. If you're actually seeing differences, then presumably it has something to do with the library typings, but without seeing it in front of me I wouldn't be able to say anything definitive.
    – jcalz
    Feb 3, 2021 at 19:49
1

typeof fn lets you get the type of a function, so Parameters<typeof EventEmitter.prototype.emit> should get things working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.