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

Stronger 'this' warnings - suggestion #18436

Closed
avivcarmis opened this issue Sep 13, 2017 · 5 comments
Closed

Stronger 'this' warnings - suggestion #18436

avivcarmis opened this issue Sep 13, 2017 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@avivcarmis
Copy link

Would it be possible to add (or did I miss an already open issue regarding) support for a stronger this assertion?

Coming from strongly typed languages I have to say that worrying about the this context adds a lot of noise to coding.

I've read this in typescript wiki page and it helps identifying the issue a lot, but I really don't feel safe coding when this issue is only identified in runtime.

The wiki page suggests red flags for this issues, is there a chance of requesting this support by the compiler? Some strict mode flag that makes the compiler merely identify places that instance methods are passed by reference instead of invoked and place a warning/error?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 13, 2017
@RyanCavanaugh
Copy link
Member

See #513

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Sep 14, 2017

It is worth mentioning that another option is to simply avoid this entirely. Unlike the languages to which you are likely alluding, this is only necessary when you are using classes. The languages you are referring to as comparison points, likely do require the use of classes, but TypeScript does not.

Honestly, without experimental and prospective features like decorators, classes in TS/ES have relatively weak expressive power compared to plain old functions and object literals.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 14, 2017

@aluanhaddad well, while this is desirable in a class, it is still quite easy to make mistakes. For a lot of people classes are not really optional. In some cases they are preferred and in some cases, if people are using a framework, they are required. But it isn't just classes, it is any of the instance based object creation patterns you find in JavaScript, including constructor functions and factories.

I think the point is, almost all of the issues related to this usage are collated in #513 and have their respective issues. Scoping and binding of this is one of the biggest challenges for JavaScript and I don't know anyone who hasn't screwed it up now and again, and TypeScript can only help so much. The most common mistake I would see these days is something like this:

class Foo {
  value: string;
  constructor(target: HTMLElement) {
    this.value = 'bar';
    target.addEventListener('click', this.onclick);
  }
  onclick() {
    console.log(this.value);
  }
}

const button = document.createElement('button');
button.innerHTML = 'Click Me!';
document.body.appendChild(button);
const foo = new Foo(button);

When you click on the button, you won't get bar logged, you will get an empty string. The challenge is, can TypeScript help us statically identify the error? If so, there is likely an issue linked off of #513, otherwise it is up to us. this scoping can and is dangerous, even with the help of TypeScript.

@aluanhaddad
Copy link
Contributor

@kitsonk you are of correct.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 2, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants