Ignore some TypeScript compilation errors?

I am wondering if there is a way to ignore some TypeScript errors when compiling?

I basically have the same problems as most people with large projects that use this keyword, and I don't want to include all the class methods in the constructor.

So I have an example:

TypeScript Example

It seems that the correct JS is being created and allows me to get around this problem with the keyword, however, as you can see in the example, the TypeScript compiler tells me that I can not compile this code as a keyword, this is unacceptable within this scope. However, I do not understand why this is a mistake, since it gives good code.

So, is there a way to say to ignore certain errors? I am sure that given the time, there will be a good way to manage this keyword, but at present I find it quite difficult.

== Edit ==

(Do not read if you do not need the context of this question and partial ranting)

Just add some context to all of this to show that I'm not just some kind of nasty thing (I'm sure that many of you will still think that I am), and that I have good reasons why I I want to be able to get these errors through.

Here are some previous questions that I have made that highlight some of the main problems (imo) using TypeScript of this current implementation.

Using Lawn with Typescript

Problem with child reach of this in Typescript

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

The main problem I am facing is that I have to ensure that all the logic is within the agreed area, I need to have access to things in the knockout, jQuery, etc. and the local instance of the class. I used this with var self = this; in class declaration in javascript and worked fine. As already mentioned in some of these previous questions, I can’t do it now, so the only way to guarantee the scope is to use lambda methods, and the only way to define one of them as a method inside a class is inside the constructor, and this part is HEAVILY with personal preference but it seems horrifying to me that people seem to think that using this syntax is classified as a recommended pattern, not just work.

I know that TypeScript is in alpha phase, and a lot will change, and I HOPE so much that we will get a better way to handle it , but currently I am either doing everything for TypeScript to work (and it is within hundreds files that I transfer to TypeScript), or I just make a call that I know better than the compiler in this case (VERY DANGEROUS I KNOW) so that I can keep my code nice and hopefully when the best template for processing appears, I can move it.

Also just on the side of the note, I know that many people love what TypeScript embraces and tries to stay as close as possible to new JavaScript features and well-known syntax, which is great, but TypeScript is NOT the next version of JavaScript, so I don't see a problem with adding syntactic sugar into the language, as people who want to use the latest and greatest official JavaScript implementation can still do this.

+4
source share
2 answers

I think your question is the XY problem. Why can I make sure that some of my class methods guarantee the correct this context?

For this problem, I would suggest this solution:

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

This has several advantages.

First, you clearly indicate what is happening. Most programmers will probably not understand the subtle semantics of the difference between the syntax of an element and a method in terms of codegen.

Secondly, it’s very clear when looking at the constructor which methods will have this guaranteed context. Critically, in terms of performance, you don’t want to write all your methods in this way, just the ones that he absolutely needs.

Finally, it preserves the semantics of the OOP class. In fact, you can use super.DoSomething from an implementation of the DoSomething derived class.

+4
source

I am sure that you know the standard form for defining a function without an arrow. There's another TypeScript expression that generates the exact code, but without a compilation 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 not? Good on specification: an arrow function expression preserves the this of its enclosing context . Therefore, it stores the value of this from the volume that was declared. But declaring a function at the class level of this does not actually matter.

Here is an example that is incorrect for the same reason, which may be more understandable:

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

The way in which the initializer works in conjunction with the constructor is an implementation detail that you cannot rely on. This may change.

I am sure that time will be a good way to manage this keyword, but at present I find it quite difficult.

One of the high-level goals (I love) in TypeScript is to extend the JavaScript language and work with it, and not to combat it. How this works is difficult, but worth exploring.

+2
source

All Articles