Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an option in TypeScript so that a compiled JavaScript file is JSHint compatible?

Consider the following TypeScript code sample.

var checkEqual = function (a:any, b:any) {
    if(a == null)
    {
        return false;
    }
}

When I will compile it, it will generate the corresponding JavaScript file without any compile time error as below:

var checkEqual = function (a, b) {
    if (a == null) {
        return false;
    }
};

But when I run JSHint on the compiled JavaScript code/file I will have following error/warning:

Use === to compare with null

I want the compiled JavaScript code from TypeScript to be JSHint compatible (no errors and warnings should be there). That means either it should generate the correct code or it should give compile time error.

PS: I did not know about tslint before, but even after using it, it is not compiling JavaScript compatible with JSHint (JSHint is still throwing warnings).

like image 810
sumi Avatar asked Aug 22 '16 10:08

sumi


1 Answers

It's simply not possible! TypeScript was never built with the goal to create code that is compliant with a linter. When using TypeScript you have to think of the generated JavaScript as bytecode. There is no reason to look at your bytecode.

Some linters may even bother you with problematic use of this. Which in fact should be used very carefully in ES5. But when using TypeScript/ES2015 class definitions you need to use this a lot and transpiling to ES5 will create a lot of them. So creating code that matches your linter will force you to not use a lot of features of TypeScript.

So if you want to lint bytecode, then you should write bytecode ;)

like image 186
DaSch Avatar answered Sep 26 '22 06:09

DaSch