-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
This-function parameter inference fails in object with other typing features #10461
Comments
I think some background story will be helpful. Annotating declare class VueTyped<T> {
data<D>(d: D): VueTyped<T & D>;
method<M>(m: M & { [k:string]: (this: T) => any }): VueTyped<T & typeof m>
static new(): VueTyped<{}>
get(): T
}
var b = VueTyped.new()
.data({msg: '123'})
.method({
method() {
return this.msgs // expected to have an error for typo
}
})
.get() on the line |
For your In Maybe @ahejlsberg has some insights. |
Thanks for pointing out the spec. I understand It seems For Vue specific issue, I found constraints on type parameter is not ignored. So it can be used to annotate declare class VueTyped<T> {
method<M extends { [k:string]: (this: T) => {} }>(m: M): VueTyped<T & M>
} |
The issue here is fixed by #9746. Your example works in that branch, using either a union type for the This is a pretty neat trick. It is kind of magic how the correct type just pops out at the end. One thing I noticed is that you get errors if you add additional parameters to the methods you define with declare class VueTyped<T> {
data<D>(d: D): VueTyped<T & D>;
method<M>(m: M & { [k:string]: (this: T, ...args: any[]) => any }): VueTyped<T & M>
static new(): VueTyped<{}>
get(): T
} One problem that remains is that when you add explicit parameter type annotations to methods in the Adding @sandersn. |
Should be fixed now that #9746 is merged. |
@HerringtonDarkholme have you thought about porting this into to the Vue declaration file at all? |
@DanielRosenwasser @kaorun343 has already updated declaration file for Vue 2.0 and that declaration file has used a lot of TS2.0 features to give a description for Vue. My approach needs many API changes which are unsuitable for official Vue to adopt. |
This may relate to #10288
TypeScript Version: 2.0.0, installed via typescript@beta
Code
All code under
--noImplicitThis
flag.Expected behavior:
All three example should compile, if
this
parameter is inferred on call siteOr no example should compile, if
this
must be explicitly annotatedActual behavior:
Only the first example compiles.
The text was updated successfully, but these errors were encountered: