spread syntax in typescript

Viewed 1062

I'm trying to extend a class in an Angular project and I want to pass arguments to the parent class using the rest/spread syntax of ES2015 like the answer to this question

constructor(...args) {
    super(...args);
}

but I get an error Supplied parameters do not match any signature of call target and my app won't compile.

I made a contrived example using the typescript playground. Does anyone know how to get this to work without having to explicitly write the arguments of the parent class methods?

UPDATE: As pointed out by Teddy Sterne I actually don't need to use this for the constructor because it has the same arguments. I actually want to use this for a method that returns an observable. I want to catch errors by overriding the method and returning super.parentMethod(...args).catch(/* deal with error */)

3 Answers

Another workaround would be to use the built-in ConstructorParameters Type Helper.

class A {
    constructor (foo: number, bar: string) {}
}


class B extends A {
    constructor (...args: ConstructorParameters<typeof A>) {
        super(...args);
    }
}
Related