Closed
Description
Migrated issue from codeplex: https://typescript.codeplex.com/workitem/507
Currently typescript will type the "this" pointer in function callbacks as "any." Arrow syntax lets us capture "this" from the outer scope, and this can enable proper typing. It would be nice to be able to provide
an optional "ambient this" declaration in function signatures:
(declare this : MyType, first: number, second: string) : any;
The rules would be:
- the declaration is obviously optional
- if specified, should be first parameter
- cannot be used with arrow syntax lambdas (should be compiler error)
I know you can cast as needed to get the intellisense but I'd rather not put this responsibility on the implementer of the function. Ideally it should be part of the definition.
Example with Sammy:
var app = $.sammy("#view", function() {
// define default route
this.get("#/", function() { ... }
});
In the above case, typescript can only type this as any.
Thoughts?