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

generics for "this" type #8740

Closed
pleerock opened this issue May 22, 2016 · 1 comment
Closed

generics for "this" type #8740

pleerock opened this issue May 22, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@pleerock
Copy link

Need to allow to use generics for "this" type, like this: this<T>

class Collection<T> extends Array<T> {

    map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): this { 
        // this method will return this<T>, but I want to return this<U>
        return new (this.constructor as any)(...super.map(callbackfn, thisArg));
    }

}

example of usage:

new Collection(
    new Model(1, "Mercedes"),
    new Model(2, "BMW"),
    new Model(3, "Ford")
)
    .map(model => model.engine)
    .forEach(engine => engine.serialNumber); // here compiler should see engine.serialNumber
@DanielRosenwasser
Copy link
Member

Looks like a duplicate of #6223. Let me know if not and we can reopen this.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label May 22, 2016
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants