Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super constructor return ignored #7710

Closed
whitneyit opened this issue Mar 28, 2016 · 3 comments
Closed

Super constructor return ignored #7710

whitneyit opened this issue Mar 28, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@whitneyit
Copy link
Contributor

TypeScript Version:

nightly (1.9.0-dev.20160313)

Code

class Collection extends Array {

    constructor(...args) {
        const first = arguments[0];
        if (args.length === 1 && typeof first === 'number') {
            super(first, null);
            this.pop();
        } else {
            super(...args);
        }
    }

}

const a = new Collection(3, 2, 1);
const b = new Collection(3);
console.log(a, b);

Expected behavior:
The Array is correctly subclassed and the instances are created correctly.

Actual behavior:
The instances are not created correctly.

Notes:

Questions:

  • Should this be part of core?
interface ArrayConstructor {
    new (...items: any[]): any[];
}
@whitneyit
Copy link
Contributor Author

The following can be used as a workaround:

class Collection extends Array {

    constructor(...args) {
        super();
        const first = arguments[0];
        if (args.length === 1 && typeof first === 'number') {
            super.push.call(this, first);
        } else {
            super.push.apply(this, args);
        }
    }

}

const a = new Collection(3, 2, 1);
const b = new Collection(3);
console.log(a, b);

@jeffreymorlan
Copy link
Contributor

This code works in the babel repl.

Actually, it doesn't - it's impossible to subclass builtin classes like Array in ES5, because there's no way to tell the Array constructor to use your subclass's prototype.

If you transpile this with Babel, new Collection creates a plain array which does not inherit from Collection.prototype, defeating the whole purpose of subclassing.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Mar 28, 2016
@mhegazy mhegazy closed this as completed Mar 28, 2016
@Arnavion
Copy link
Contributor

(Since it wasn't explicitly answered) The reason the compiler complains about the two super() calls is that Collection extends Array and not Array<T>. The construct signature that takes in array elements only exists in the generic form new<T>(...items: T[]): T[] .

So Collection should either be class Collection<T> extends Array<T> or class Collection extends Array<any> depending on what the intended use is.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants