Skip to content

Include function this argument in function signature #9069

Closed
@iskiselev

Description

@iskiselev

In JavaScript each function, except normal arguments has a special call-time bound this argument. TypeScript greatly helps to find an error when any of normal arguments passed into function is incorrect, but currently doesn't help to find an error when function called against incorrect this. It is even covered in FAQ.
If only TypeScript calculate (and allow override) this type for function, and treat it as additional argument when compare functions type, all such problems would be mitigated.
If function doesn't use any this fields, such additional argument simply should have any type.

Here is an example of source with such problem:

class SampleClass{
  field : string;
  f(){ return this.field};
}

var instance = new SampleClass;
var detachedF = instance.f;
detachedF();

In that example, TypeScript has enough info to infer, that detachedF could be called only over interface {field : string} type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions