Ignore certain TypeScript compile errors? Ignore certain TypeScript compile errors? typescript typescript

Ignore certain TypeScript compile errors?


The author's specific issue with this seems to be solved but the question is posed about ignoring errors, and for those who end up here looking how to ignore errors:

If properly fixing the error or using more decent workarounds like already suggested here are not an option, as of TypeScript 2.6 (released on Oct 31, 2017), now there is a way to ignore all errors from a specific line using // @ts-ignore comments before the target line.

The mendtioned documentation is succinct enough, but to recap:

// @ts-ignoreconst s : string = false

disables error reporting for this line.

However, this should only be used as a last resort when fixing the error or using hacks like (x as any) is much more trouble than losing all type checking for a line.

As for specifying certain errors, the current (mid-2018) state is discussed here, in Design Meeting Notes (2/16/2018) and further comments, which is basically

"no conclusion yet"

and strong opposition to introducing this fine tuning.


I think your question as posed is an XY problem. What you're going for is how can I ensure that some of my class methods are guaranteed to have a correct this context?

For that problem, I would propose this solution:

class LambdaMethods {    constructor(private message: string) {        this.DoSomething = this.DoSomething.bind(this);    }    public DoSomething() {        alert(this.message);    }}

This has several benefits.

First, you're being explicit about what's going on. Most programmers are probably not going to understand the subtle semantics about what the difference between the member and method syntax are in terms of codegen.

Second, it makes it very clear, from looking at the constructor, which methods are going to have a guaranteed this context. Critically, from a performance, perspective, you don't want to write all your methods this way, just the ones that absolutely need it.

Finally, it preserves the OOP semantics of the class. You'll actually be able to use super.DoSomething from a derived class implementation of DoSomething.


I'm sure you're aware of the standard form of defining a function without the arrow notation. There's another TypeScript expression that generates the exact same code but without the compile error:

class LambdaMethods {    private message: string;    public DoSomething: () => void;    constructor(message: string) {        this.message = message;        this.DoSomething = () => { alert(this.message); };    }}

So why is this legal and the other one isn't? Well according to the spec: an arrow function expression preserves the this of its enclosing context. So it preserves the meaning of this from the scope it was declared. But declaring a function at the class level this doesn't actually have a meaning.

Here's an example that's wrong for the exact same reason that might be more clear:

class LambdaMethods {    private message: string;    constructor(message: string) {        this.message = message;    }    var a = this.message;  // can't do this}

The way that initializer works by being combined with the constructor is an implementation detail that can't be relied upon. It could change.

I am sure given time there will be a nice way to manage the this keyword, but currently I find it pretty dire.

One of the high-level goals (that I love) in TypeScript is to extend the JavaScript language and work with it, not fight it. How this operates is tricky but worth learning.