Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-invalid-this should allow this in assigned methods #1544

Closed
ghost opened this issue Sep 8, 2016 · 4 comments
Closed

no-invalid-this should allow this in assigned methods #1544

ghost opened this issue Sep 8, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 8, 2016

Bug Report

  • TSLint version: 3.15.1
  • TypeScript version: 2.1.0-dev.2016-0907
  • Running TSLint via: gulp

TypeScript code being linted

const systemLocate = System.locate,
    suffix = `?nocache=${Math.random()}`;
System.locate = function( ...args ) {
    return Promise.resolve( systemLocate.apply( this, args ) )
        .then( addr => addr + suffix );
};

with tslint.json configuration:

{
"no-invalid-this": [
            true,
            "check-function-in-method"
        ]
}

Actual behavior

the "this" keyword is disallowed outside of a class body

Expected behavior

No error.

The eslint rule with the same name allows for this references when assigning methods:

obj.foo = function foo() {
    // OK, this is in a method (this function assigns to a property).
    this.a = 0;
};

I would expect this to be valid in TypeScript as well.

Specificying an explicit this parameter does not clear the error. I would consider this an acceptable alternative, if it worked.

@englercj
Copy link

Bumped up against this today while doing the following:

MyClass.prototype.method = function () {
    return this.something; // `this` gives a tslint error for no-invalid-this
};

@adidahiya
Copy link
Contributor

we're going to deprecate this rule; do you think you still need it given the compiler option? #1861

@ghost
Copy link
Author

ghost commented Mar 24, 2017

No.

@ghost ghost closed this as completed Mar 24, 2017
@englercj
Copy link

I feel like they are different. I'm OK with implicit this when on a class method, but want to disallow this (or more specifically, implicit this) when on a non-class method.

Right now there isn't a way to enforce that with just the compiler option, it is either all explicit or all implicit. Or do I misunderstand the usage?

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants