node.jstypescripttooling

Is typescript robust enough?


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?


Solution

  • 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.