0

Is there an option to tell the TypeScript compiler to obey certain "rules" such as naming anonymous functions, always add the "use strict" declaration, and to always use braces?

For example, whenever you use the "extends" keyword for class inheritance, for instance, TypeScript will output this function:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};

My team's JSHint rules require "curly" braces around conditionals, and require named anonymous functions (technically "named functional expressions"). Basically I'd like TypeScript's transpiler to produce better quality JavaScript code, in terms of style as opposed to efficiency (although well-performing code is also important, of course).

We can use tslint to lint our TypeScript, but I'd like to also ensure that the generated code also meets certain quality standards.

2
  • How does that bring any benefit? You can easily add "use strict"; to the top of each file in a build script (and I can understand the need for that when trying to get the code to run in certain environments), but the other changes you are talking about require either parsing the javascript code and fixing the linting errors, or changing the compiler source (all this would slow down the build and be quite a lot of work to maintain); however, to partly echo what mk said in their answer, I would suggest if you're going to write code in TypeScript that you focus on the TypeScript and use tslint. Commented Dec 9, 2015 at 21:22
  • 2
    You can also use --noEmitHelpers and provide your own well-formatted __extends function somewhere at global scope. Commented Dec 9, 2015 at 22:47

1 Answer 1

4

The style standards are there to help programmers read code and avoid mistakes. You'll never be reading or otherwise interacting with compiled code, because you should have source maps. If you're never going to see it (writing, debug, or stack traces), then why does it need to look good? It doesn't, so this isn't implemented.

What you can and should do, though, is set up JSHint to ignore js files produced by TypeScript.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, using named functional expressions rather than unnamed anonymous functions DOES have a production impact, though, in that the named function can be traced back to the source whereas all anonymous functions have the same name.
@JoughDempsey That extension function added by TS should never appear in a stack trace, so you're safe, right? You can still freely add a name to any typescript function, and it will be preserved. I mean, in any case, the answer is still "you can't do that, sorry", I'm just explaining why it's not implemented and what you can do instead.
You can add a name to any function that you write, but when using class inheritance TypeScript will generate unnamed anonymous functions instead of named function expressions for any generated code, including all constructors, which means that any runtime errors will be simply Anonymous. It sounds like there's no way to generate better code, though, other than submitting pull requests to the TypeScript compiler itself.
@JoughDempsey TypeScript does not generate anonymous functions or constructors, names are preserved. You should check for some sort of rogue minifier.
It sure does! Try extending class Foo: class Foo { constructor(s:string) { } } class Bar extends Foo { constructor(s:string){ } } See: typescriptlang.org/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.