4

Everyone these days is forcing typescript. There are so many fans and articles about it. Angular team is making their framework in TS. But my experience with migrating ES6 to TS was very disappointing.

I tried to migrate our relatively fresh codebase (writen in ES6) to Typescript last month and faced a ton of pitfalls!

To be clear, we are talking about node.js application with mocha unit tests and ESLint configured (using babel to transpile).

First of all, to empower type checking I set up noImplicitAny option, got hundreds of errors and fixed it. But after that, I got typing errors due to typescript does not understand some node.js predefined modules, like stream (The problem is actually bigger, due to lack of typings for a lot of modules).

After that, I installed typings - recommended replacement for tsd tool for manage library d.ts files, but it's node typing definition , while resolving stream problem, added a lot of errors because it duplicates some predefined types.

In addition, I found out that typescript actually does not compile many features of ES6 into ES5 actually, such as generators. It forced me to make complex build process (TS -> (typescript) ES6 -> (babel) ES5), and it means that I have to waste my original source maps.

All above took a lot of time to configure.

So, I'm confused. I really love the idea behind typescript, but implementation seems so rude to me. I hope I'm wrong.

Maybe someone who used Typescript in real project, not HelloWorld one, could explain me what am I doing wrong?

Elessar.perm
  • 751
  • 1
  • 9
  • 18

1 Answers1

5

I set up noImplicitAny option

You have very high expectations. For a better experience while migrating a project from ES6, just don't use this option.

I got typing errors due to typescript does not understand some node.js predefined modules, like stream.

The JS libraries that don't have type definitions are a pain. But the implicit type any will save you.

After that, I installed typings […] added a lot of errors because it duplicates some predefined types.

Exclude the browser directory and browser.d.ts from your tsconfig.json.

In addition, i found out that typescript does not compile many features of ES6 to ES5 actually, such as generators. It forces me to made complex build process (TS -> (typescript) ES6 -> (babel) ES5), and it means that I have to waste my original source maps.

It's the design choice that makes TS really robust: the compiled code doesn't need any runtime library.

But why do you use Babel on your generated ES6 code? With Node.js 4 or 5, your ES6 code will work fine.

Is typescript robust enough?

As much as the JavaScript VM are.

TypeScript with the target ES6 on Node.js

Since TS 1.7, the option --module can be used in association with the target es6. Example, in tsconfig.json:

"compilerOptions": {
  "module": "commonjs",
  "target": "es6",
}

NB: Since TS 1.8, modules are emitted with a "use strict"; prologue.

Paleo
  • 21,831
  • 4
  • 65
  • 76
  • Thank you for your answer, @Paleo! I had no high expectations about `noImplicitAny`, just configured it temporary in order to refactor my ES6 code with type definitions. So it is not problem at all. About node v4-5 - it does allow ES6 to run, but with restrictions such as making `strict mode` necessary for `let` keyword or something. Also, it is not possible to use ES6 Modules with pure Node.js now. Am I missing something? – Elessar.perm Apr 23 '16 at 11:18
  • 2
    Node 5 has flags to allow not requiring strict. These are the flags I use to get Node 5 to work properly with Typescript `--use_strict --harmony --harmony_default_parameters --harmony_destructuring`. Though with Node 6 about to release, much of that will become default. – ArcSine Apr 23 '16 at 11:28
  • @Elessar.perm, since TS 1.7, you can use the option `--module` in association with the target `es6`. – Paleo Apr 23 '16 at 15:02
  • Thank you guys, it is finally clear to me how to cook this stuff! – Elessar.perm Apr 23 '16 at 15:19