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 theParameters
are tosuper.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
?