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

[Feature Request] A way to instruct how to infer this #15836

Closed
bigopon opened this issue May 15, 2017 · 4 comments
Closed

[Feature Request] A way to instruct how to infer this #15836

bigopon opened this issue May 15, 2017 · 4 comments
Labels
Docs The issue relates to how you learn TypeScript

Comments

@bigopon
Copy link

bigopon commented May 15, 2017

TypeScript Version: 2.3.2

Code

I have this definition:

extend<T, K>(cls: (...args: {}[]) => T, xtn: K): new (...args: {}[]) => T & K

Usage:

var NewClass = extend(MyClass, {
  init(a) {
    this.doSomething();
  },

  // ....
})

The NewClass will still have all expected method init ..., but the this cannot be inferred inside the extension object.

It would be great if there is a way to instruct Typescript how to infer the this inside extension object based on the first passed param meter. (MyClass in the example). The reason is to have better intellisense.

@DanielRosenwasser
Copy link
Member

We actually shipped this in TypeScript 2.3 to support APIs like those in Ember and Vue.

See #14141.

@DanielRosenwasser
Copy link
Member

For the record, you probably want

extend<T, K>(cls: (...args: {}[]) => T, xtn: K & ThisType<T & K>): new (...args: {}[]) => T & K

@DanielRosenwasser DanielRosenwasser added Fixed A PR has been merged for this issue Suggestion An idea for TypeScript labels May 15, 2017
@DanielRosenwasser DanielRosenwasser added Docs The issue relates to how you learn TypeScript and removed Fixed A PR has been merged for this issue Suggestion An idea for TypeScript labels May 15, 2017
@bigopon
Copy link
Author

bigopon commented May 15, 2017

It looks awesome. But I couldn't make it work. Tried adding noImplicitThis to the jsconfig but i got warning that it is not a valid config.

And makes me wonder if the following is doable

interface Observable {
  /**
   * Very long long long comment to explain how to use `options`
   */
  on(event: string, handler: Function, options?: object): Observable
}

interface MyClass extends Observable {
  // ...
}

We know that using new MyClass().on(...) will return MyClass instance, but how to tell Typescript to return MyClass instead of Observable. I don't wanna rewrite all the comment on the overload method on MyClass interface.

Edit: My fault on some interface declaration. it works perfect.

@kitsonk
Copy link
Contributor

kitsonk commented May 15, 2017

interface Observable {
  /**
   * Very long long long comment to explain how to use `options`
   */
  on(event: string, handler: Function, options?: object): this
}

interface MyClass extends Observable {
  // ...
}

@bigopon bigopon closed this as completed May 25, 2017
@bigopon bigopon reopened this May 25, 2017
@bigopon bigopon closed this as completed Jun 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants