Typescript error when passing arguments to super class. `A spread argument must either have a tuple type or be passed to a rest parameter (TS2556).`

Viewed 1610

The following gives error TS2556, how do I fix it?

class Test {
    constructor(x: number) {}
}

class Test2 extends Test {
    constructor(...args) {
        super(...args); // TS2556
    }
}

Or if you are using jsdoc with tsc for typechecking:

class Test {
    /**
     * @param {number} x
     */
    constructor(x) {}
}

class Test2 extends Test {
    constructor(...args) {
        super(...args);
    }
}
1 Answers

Use ConstructorParameters<T>, if you are calling a function you can get away with just Parameters<T>.

class Test {
    constructor(x: number) {}
}

class Test2 extends Test {
    constructor(...args: ConstructorParameters<typeof Test>) {
        super(...args);
    }
}

Or for jsdoc:

class Test {
    /**
     * @param {number} x
     */
    constructor(x) {}
}

class Test2 extends Test {
    /**
     * @param  {ConstructorParameters<typeof Test>} args
     */
    constructor(...args) {
        super(...args);
    }
}
Related