TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Proposal to improve static analysis for the thisArg of a function

Open nathggns opened this issue 11 years ago • 54 comments

This is an official proposal for one of the issues outlined in #229.

Firstly, I'd like to give a big thank you to @ahejlsberg for identifying the issue, and to @redexp, @ivogabe, @cspotcode, and others for their contributions to the original issue as they have formed most of the grunt work of this proposal. I've just decided I want the feature enough that I will take the time to write an official proposal.

I'd also like to note this is my first official proposal (for not just TypeScript, any language). I'm sorry if I've missed anything glaring out. Any advice on how to improve my proposals would be appreciated.

The Problem

When working with many libraries that take callbacks (including jQuery), TypeScript will give very little intelligence as to the type of the thisArg inside a callback's body – intact, it will simply be typed as any.

A few examples follow

// A standard jQuery example. While jQuery does
// also provide the value of `thisArg` as a argument
// to the callback, it is still helpful to provide a familiar example 
$('body').on('click', function() {
    this // any?
});

// The example from the original issue
// There would be no intellisense in this function, and TypeScript will
// just assume everything is okay even if we misspell get or Sammy's API changes
// because this is typed to any. 
$.sammy('#view', function() {
    this.get('#/', function() { ... });
});

// Here is a typescript example
// myClickHandler obviously relies on its thisArg being a HTMLElement
// but nothing stops code from calling it with any thisArg either
// directly (myClickHandler(...)) or with call/bind (myClickHandler.call(...))
function myClickHandler(event : MouseEvent) {
    this.innerHTML = [event.pageX, event.pageY].join(', ')
}

document.body.addEventListener('click', myClickHandler);

Because the thisArg is of type any, we get no type checking and no autocomplete or any other kind of intellisense. Also, when defining functions within TypeScript user land code that rely on the thisArg being a certain type, there is nothing stopping consuming code calling it with whatever type they provide.

Proposed Change

Ideally, a function definition would be able to define the type of its thisArg. This means that any consuming code must provide a thisArg of this type (using call, apply, or bind). It also means that within the body of that function, we can assume that the type of its thisArg is of the provided type, rather than reverting to an any type.

Quick Note: None of this applies to functions defined using arrow functions. You cannot specify the type of the thisArg with arrow functions as their type is lexically bound. I believe if you try to use the syntax specified below with arrow functions it should be either a compiling error or a parsing error.

There are many different places where you can define a function in TypeScript. However, the main syntax is as follows at the moment.

function someFunction(someArg : someType) : someResult { ... }

I propose adding to this syntax a way of specifying the type of the thisArg within the function body. There are a few suggestions for this syntax including some from the original issue and some I have thought of myself, but I think the best out of the suggested is the following syntax:

// Function Statement
function someFunction<this : SomeThisType>(someArg : SomeArgType) : SomeReturnType {
    // this is of type SomeThisType
}

// Function expression
var someFunction = function<this : SomeThisType>(someArg : SomeArgType) : SomeReturnType {
    // this is of type SomeThisType
}

// Function argument
// This would work similairly for declared functions (declare function ...) in definition files
function someFunction(someCallback : <this : SomeThisType>(someArg : SomeArgType) => SomeReturnType) : SomeReturnType { .. }

someFunction(function() {
    // this is of  type SomeThisType
});

// Interface function argument

interface SomeFunctionArgument {
    <this : SomeThisType>(someArg : SomeArgType) : SomeReturnType;
}

function someFunction(someCallback : SomeFunctionArgument) : SomeReturnType { ... }

someFunction(function() {
    // this is of  type SomeThisType
});

In the statement and expression examples, we can assume that the type of thisArg inside their bodies is SomeThisType. In the last two examples, we can assume that type type of thisArg inside the passed callbacks is of type SomeThisType.

However, these assumptions rely on enforcing that any TypeScript code calling these functions have to pass the correct thisArgs. Calling functions in JavaScript can get very complicated but here are the different ways that you can pass a thisArg, along with the semantics of this proposal.

Taken the following code:

class SomeType {
   doStuff() { ... }
}

var method = function<this : SomeType>() {
    this.doStuff();
};

We would no longer be able to call this function simply by invoking it, as the thisArg would be set to the global object instead of an instance of SomeType. Trying to do so should throw a TypeError of some description (the exact error is intentionally left undecided at this point).

method(); // Error: Window cannot be converted to SomeType ...

Specifying the type of thisArg using call, apply, or bind with an incompatible type will throw the same error. With call and apply the error is triggered at call time. With bind, it is triggered at "bind time" (to more easily find the source of an error).

method.call({});
method.apply({});
method.bind({})();

Only invoking with a compatible type of SomeType will be accepted by the type engine.

method.call(new SomeType());
method.apply(new SomeType());
method.bind(new SomeType())();

Again, I would like to stress that this proposal has no affect on arrow functions. Even attempting to specify the thisArg for arrow functions should be an error.

var someFunction = <this : SomeType>() => { ... }; // Error

One notably exception to this is that you are able to pass arrow functions as arguments where a thisArg has been specified.

Example:

declare function someFunction(someCallback : <this : SomeType>() => number);

someFunction(() => 1);

Note: bind does not change the specified thisArg of a function. This can only be set at define-time.

The Syntax

You will have noticed that we pass the thisArg within the type arguments of a function, where you would normally pass generic type information. This does not conflict however, as the thisArg must be the last type argument, separated by a , if any other arguments are passed, and must be prefixed with this: as follows.

class SomeType<T> {
    constructor(public someProp : T) {}
}

function someFunction<T, this : SomeType<T>>() : T {
    return this.someProp;
}

someFunction(); // Error
someFunction.call({}); // Error
someFunction.call(new SomeType<number>(1)); // 1

Note: This is a very contrived example, as very rarely will a function ever take a generic argument as well as specifying the thisArg as specifying the thisArg is usually done when declaring function arguments and then passing anonymous functions like so:

declare someFunction(someCallback : <this : SomeType>() => number) : number;

someFunction(function() {
    // this is of type SomeType
});

Why this syntax?

I chose this syntax as the main use case for this feature is for anonymous functions, in which you almost if not never have generic type arguments for. Alternatives were to add the this into the parameter list, which I disliked as it made for a messy-looking solution in the common use case.


declare function someFunction<this : SomeType>(someArg : SomeArgType): any;

// vs

declare function someFunction(this : SomeType | someArg : SomeArgType): any;

Personally, I find the latter much harder to read.

How is this problem solved?

With this change, you can now specify the thisArg within function bodies. While this works for any functions, the main use case is for passing anonymous functions to library code. With this change, you can now specify in a declaration the thisArg in the body of a function argument.

This is a very basic example of how the on function could be written in a jQuery definition file. I've put the definition and usage in one file for convenience.

Note how in this example, the whole semantics of enforcing the type of thisArg when calling a function is irrelevant as we're simply telling TypeScript how existing JavaScript works. This is actually the common use case for this code.

// Definition
interface jQueryStatic {
    (...any) : jQueryStatic; // Simple call definition. 
    on(eventName : string, callback : <this : HTMLElement>() => any) : jQueryStatic;
}

var $ : jQueryStatic;

// Usage

$('body').on('click', function() {
    // this is now of type HTMLElement
});

Void thisArg

In some cases, the value of thisArg can be undefined (directly invoking a function in strict mode). It can be helpful to disallow consuming code from being able to use the value of this even if it isn't undefined, by making TypeScript assume it is undefined.

You can do this like so:

declare function someFunction(arg : <this : void>() => void);

someFunction(function() {
    // this === undefined
});

Classes

Currently, explicitly setting the type of thisArg in class methods is unsupported (triggering a syntax error) as doing this comes with its own set of problems, and can be added on at a later date once the basic semantics have been sorted.

This also means you cannot dynamically change a method on a object to a function that has a specified thisArg. Also, to maintain currently functionality, TypeScript will continue to assume that this is of the type in which the method is defined within the method body, but will allow you to trigger it with any thisArg.


class Foo {
    someMethod() {
        // this is assumed to be of type Foo
    }
}

var foo = Foo();
var method = foo.someMethod;

// All okay
foo.someMethod();
someMethod.call(foo);
someMethod();
someMethod.call({});
someMethod.apply({});
someMethod.bind({})();

// Not okay

foo.someMethod = function<this : number>() { .. };

Automatically setting the thisArg within class methods

In the original issue, @cspotcode suggested that the thisArg automatically be set for class methods as such that the following is an error.

class SomeType {
    someMethod() { ... }    
}

(new SomeType()).someMethod.call({});

I have intentionally left this out of this proposal as it causes a lot of its own backwards incompatibility issues, hence why there is no syntax addressed for setting the thisArg for class methods. However, should this proposal be accepted, then it might be worth extending it to address this. Should you choose to do this, this comment should be of some help to you.

Interfaces

While specifying a thisArg type for function interfaces is allowed, trying to do the same for methods or properties is not. This is something that can be revisited if/when class method support for thisArg type specifications is added.

interface SomeFunctionInterface {
    <this : number>(): number;
}

declare function numberMap(arr : number[], transformer : SomeFunctionInterface) : number[]; // Okay

interface SomeOtherInterface {
    numberMapper : SomeFunctionInterface; // Error
}

Assignability

If B is assignable to A, function<this:B> is assignable to function<this:A>, but not vice versa.

function<this:any> is assignable to function<this:A> andfunctionthis:B.functionthis:Aandfunctionthis:Bis assignable tofunctionthis:any`.

class A {}
class B extends A {}
var x = function<this : number>(){};
var y = function<this : string>(){};
var z = function<this : A>(){};
var zz = function<this : B>(){};
var a = function<this : any>(){};

x = y; // Error
y = x; // Error
zz = z; // Error

z = zz; // Okay
x = a; // Okay
y = a; // Okay
z = a; // Okay
zz = a; // Okay

a = x; // Okay
a = y; // Okay
a = z; // Okay
a = zz; // Okay

Updating lib.d.ts

@cspotcode also showed that updating lib.d.ts to properly set the thisArg could cause a lot of breakage. Again, to simplify this proposal, I have left this out. Again, this is something I think should be addressed after choosing to merge this proposal, and this feature is still useful without updating lib.d.ts.

Emitted JavaScript

Regardless of the syntax used to specify the thisArg, it must be stripped from the outputted JavaScript. This is simply a syntactical addition to the type engine, which is always stripped from emitted javascript.

Incompatibilities

I won't claim to know of every feature currently proposed for ES6 and ES7 / current TypeScript proposals, but I don't believe that the proposed syntax conflicts any proposed feature.

Breaking Changes

There are no breaking changes by simply adding this feature (as functions must manually add an annotation for the type of their thisArg). Breaking changes would only be introduced should this be added to lib.d.ts, or if classes begin to automatically set their thisArg.

nathggns avatar Feb 09 '15 03:02 nathggns

I would say "this" has enough semantics attached to it for any programmer that that's a pretty unlikely interpretation. I suppose an alternative is something like the extensions syntax used by C#:

setTimeout(callback: (window: this Window, ...args: any[]) => void, timeout?: number, ...args: any[]): number;

But it seems silly to have to name the parameter (and with Extension methods in C# you don't use "this" inside the function body).

TBH if it were entirely up to me I'd actually simply change the meaning of 'this' to match that of C#, and introduce a different term for the cases where you need the existing semantics inside a function (though I'm not quite sure what I'd call it, and frankly I'd rather always use a named parameter).

wizofaus avatar Apr 15 '15 05:04 wizofaus

Yeah, the callback literally does "receive" this object as a parameter. That's how it works for all functions. It just happens that there are different rules for how you pass the value of this, it doesn't appear in the arguments array, and there's a different default value in non-strict mode. Otherwise it's basically just another argument.

Plus, TypeScript intends to add a type system onto vanilla ECMAScript, which means not covering up JS semantics. All TypeScript developers must understand how this works, for better or worse.

As a reminder, syntax is not at all preventing the Typescript team from implementing this feature. They still need a complete spec that addresses the specific semantic issues @RyanCavanaugh has mentioned.

cspotcode avatar Apr 15 '15 14:04 cspotcode

"TypeScript intends to add a type system onto vanilla ECMAScript, which means not covering up JS semantics" Why does it mean that? TypeScript is, to me, just a "better" language based on JavaScript, that happens to compile to JavaScript. If it continue to propagate things that make JavaScript difficult to code well with then it's failed in being an improvement. Understood about the complete spec - my main concern is still some sort of "smart" detection that warns when it appears you're probably misusing "this", i.e. when the method/field you're referring to after "this." is one in the current class. Even with full typing for 'this' such a mistake is possible.

wizofaus avatar Apr 15 '15 22:04 wizofaus

I'm just saying that, realistically, proposals that stray too far from the TS developers' goals are likely to be rejected. They want to align with ECMAScript 6, adding a type system and tooling on top of Javascript, allowing easy interop with existing Javascript libraries. They intentionally avoid overly-complex code transformations. It's fairly easy to migrate existing codebases to TypeScript because they've focused on a type system and tooling, not on changing language semantics. Even the current arrow functions and classes compile into ES6 arrow functions and classes.

If the goal is merely a far better language, one might argue you should write C# and compile that into JS.

cspotcode avatar Apr 15 '15 22:04 cspotcode

For syntax I'd propose

declare class MyClass {
    someMethod(x: integer);
}

var x = function[MyClass](x: integer) {
   // this: MyClass
   this.someMethod(x);
}

And for generics:

declare class OtherClass<T> {
    someMethod(arg: T);
}

var y = function<T>[OtherClass<T>](x: T) {
    // this: OtherClass<T>
    this.someMethod(x);
}

About type system (roughly): this type should be strictly co-variated since we can not assign to this. And that is why in TS it can't be an additional parameter.

Artazor avatar Apr 16 '15 10:04 Artazor

Sorry for arriving late at this, as the biggest "item" is the ability to do essentially late binding of typing to allow chaining. I have seen a fair few machinations, but maybe we are missing something that would not impede compatibility with JavaScript/EcmaScript and would be essentially very much TypeScript without challenging generic usages. Admittedly, I am not at all familiar with the sort of hoops that this "late" typing would have on the compiler, but what about the following syntax:

interface A {
    foo(): typeof this;
}

class B implements A {
    foo(): typeof this {
        return this;
    }
}

class C extends B {
    bar(a: typeof this): typeof this {
        return a.foo();
    }
}

let c = new C();
c.foo().bar(c).foo();  /* should be totally valid */

So essentially, instead of a version of generics, or something else, whenever you need to type something of the current class (or validate that something is implemented an interface properly) you would use typeof this.

While, I haven't worked through all the emitting of the above, I think it could easily be determined and that the "reserved" type of typeof this would always be determined within the context of block it was existing in, meaning it can be scoped properly.

kitsonk avatar Jun 08 '15 13:06 kitsonk

@kitsonk I think that suggestion belongs in #285.

My vote is for this as the first argument, without a special separator. It seems to more accurately represent what is happening without adding a special syntax. I think by including this : any implicitly for functions that don't specify this means assignability is already taken care of as this is treated as another argument. It should also make things easier to implement. Unfortunately this does make assignability looser than it could be (see: Function Argument Bivariance).

@nathggns: your proposal has assignability the wrong way around for z and zz:

class A {}
class B extends A {}
var z = function<this : A>(){};
var zz = function<this : B>(){};

z.call(new A()); // Okay
z.call(new B()); // Okay
zz = z // Okay

zz.call(new A()); // Error
zz.call(new B()); // Okay
z = zz; // Error

pmccloghrylaing avatar Jun 10 '15 14:06 pmccloghrylaing

FWIW, I (unaware of this issue) opened a similar one on facebook/flow#452, in my last comment proposing the same syntax that was proposed here (e.g. function add (this : number, b: number) { return this + b; }).

I think that would be the most intuitive syntax for this, and I use it in trine for annotating function types (I use _this because babel currently considers this as an illegal parameter name). This would nicely also leave room for something like a named this syntax which would make trine's paradigm more readable (and also give full control over the scoping and destructuring of this):

function add (&a : number, b : number) { return a + b; }
function vec2add (&[x1, y1], [x2, y2]) { return [ x1 + x2, y1 + y2 ]; }
// bind syntax:
[1,2]::add([5,3]) // [6, 5]

jussi-kalliokoski avatar Jun 16 '15 18:06 jussi-kalliokoski

With respect to the new ES7 "::" bind operator I'd propose

var abc = function MyClass::(x: integer) {
   // this: MyClass
   this.someMethod(x);
}

function MyClass::abc(x: integer) {
   // this: MyClass
   this.someMethod(x);
}

var abc = function<T> OtherClass<T>::(x: T) {
    // this: OtherClass<T>
    this.someMethod(x);
}

function<T> OtherClass<T>::abc(x: T) {
    // this: OtherClass<T>
    this.someMethod(x);
}

Will it lead to the breaking changes?

Artazor avatar Aug 03 '15 16:08 Artazor

It will work also with {} types:

function {count:number}::inc() {
     this.count++
}

The most difficult seems to parse the case where type should be enclosed into parentheses:

function (()=>void)::inc() { ... }
function (A | B)::test() { ... }

And even

function<T> (T::(x: number)=>number)::bar(y: number, t: T): number {
     return t::this(y) + y
}

var foo = function {factor:number}::(x: number) { return this.factor*x*x; }
console.log(foo::bar(5, {factor: 0.1})) // 7.5

However it seems that here we need not more than two or three look-ahead tokens. Need we?

Artazor avatar Aug 03 '15 16:08 Artazor

Is there any official progress/comments on this?

nathggns avatar Aug 06 '15 19:08 nathggns

@nathggns see https://github.com/Microsoft/TypeScript/issues/3694 for the latest proposal

danquirk avatar Aug 06 '15 19:08 danquirk

this is now tracked by #3694

mhegazy avatar Sep 16 '15 22:09 mhegazy

+1

Zorgatone avatar Sep 28 '15 07:09 Zorgatone