Ignore certain TypeScript compile errors?

Grofit picture Grofit · Apr 15, 2013 · Viewed 20.3k times · Source

I am wondering if there is a way to ignore certain TypeScript errors upon compilation?

I basically have the same issues most people with large projects have around using the this keyword, and I don't want to put all my classes methods into the constructor.

So I have got an example like so:

TypeScript Example

Which seems to create perfectly valid JS and allows me to get around the this keyword issue, however as you can see in the example the typescript compiler tells me that I cannot compile that code as the keyword this is not valid within that scope. However I don't see why it is an error as it produces okay code.

So is there a way to tell it to ignore certain errors? I am sure given time there will be a nice way to manage the this keyword, but currently I find it pretty dire.

== Edit ==

(Do not read unless you care about context of this question and partial rant)

Just to add some context to all this to show that I'm not just some nut-job (I am sure a lot of you will still think I am) and that I have some good reasons why I want to be able to allow these errors to go through.

Here are some previous questions I have made which highlight some major problems (imo) with TypeScript current this implementation.

Using lawnchair with Typescript

Issue with child scoping of this in Typescript

https://typescript.codeplex.com/discussions/429350 (And some comments I make down the bottom)

The underlying problem I have is that I need to guarantee that all logic is within a consistent scope, I need to be able to access things within knockout, jQuery etc and the local instance of a class. I used to do this with the var self = this; within the class declaration in JavaScript and worked great. As mentioned in some of these previous questions I cannot do that now, so the only way I can guarantee the scope is to use lambda methods, and the only way I can define one of these as a method within a class is within the constructor, and this part is HEAVILY down to personal preference, but I find it horrific that people seem to think that using that syntax is classed as a recommended pattern and not just a work around.

I know TypeScript is in alpha phase and a lot will change, and I HOPE so much that we get some nicer way to deal with this but currently I either make everything a huge mess just to get typescript working (and this is within Hundreds of files which I'm migrating over to TypeScript ) or I just make the call that I know better than the compiler in this case (VERY DANGEROUS I KNOW) so I can keep my code nice and hopefully when a better pattern comes out for handling this I can migrate it then.

Also just on a side note I know a lot of people are loving the fact that TypeScript is embracing and trying to stay as close to the new JavaScript features and known syntax as possible which is great, but typescript is NOT the next version of JavaScript so I don't see a problem with adding some syntactic sugar to the language as people who want to use the latest and greatest official JavaScript implementation can still do so.

Answer

stsloth picture stsloth · Aug 11, 2018

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-ignore
const 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.