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
Function with this
type can be assigned to a type without
#22289
Comments
Also fails on 2.7.2 |
Note that this issue has slightly broader consequences than the situation sketched above. Because by default class methods don't auto-infer a function twice(f: () => number) {
alert(2 * f())
}
class C {
constructor(private v: number) { }
get() { return this.v }
}
const c: C = new C(2);
// Both typecheck:
twice(() => c.get()); // Will alert 4
twice(c.get); // Will alert NaN |
The error is not reported even if the option "strict" is enabled. declare const method: (this: Date) => void
declare let f: () => void
f = method // no error in TS 2.8-dev |
Looks like a duplicate of #7968. |
@mattmccutchen That seems to be about a flag to change the default |
@hesselink As #7968 suggests, the current behavior is that when the |
Ah you're right. I guess I'll close this ticket and subscribe to that one. |
TypeScript Version: Version 2.8.0-dev.20180302
Search Terms: strict this assigned property
Code
The following code, compiled with
--strict
, gives a type error:This correctly prints "error TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'C'."
However, I can assign a type to the variable
f
without thethis
context, and things will typecheck (but fail if the function actually usesthis
internally):Expected behavior:
The code gives a type error like "Type '(this: C) => void' is not assignable to type '() => void'.
Actual behavior:
The code compiles without errors.
Playground Link: here
Related Issues:
The text was updated successfully, but these errors were encountered: