Skip to content
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

Make implicit this part of function signature. #29669

Closed
5 tasks done
Akhristenko opened this issue Jan 31, 2019 · 1 comment
Closed
5 tasks done

Make implicit this part of function signature. #29669

Akhristenko opened this issue Jan 31, 2019 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Akhristenko
Copy link

Search Terms

implicit this method signature

Suggestion

Include implicit this in method signature when this used in method body and we can infer this type.

Use Cases

This help us find cases when we pass method of object as callback without using bind.

Examles

Let we have current class, function and class instance:

class Test {
    str = "string";
    impl() {
        return this.str;
    }
    expl(this: Test) {
        return this.str;
    }
}

function test(cb: (this: void) => string) {
    alert(cb());
}

let obj = new Test();

If we write test(obj.expl) we get compilation error.
In this code this inside impl() function infer to Test type
but if we write test(obj.impl) we don't get compilation error and get runtime error.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@ahejlsberg
Copy link
Member

Duplicate of #7968.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants