Christopher Klewes
Christopher Klewes

Reputation: 11435

How to introduce typescript on a big nodeJS code basis step by step?

I do own quite a big code basis implemented in JavaScript based on NodeJS. I do want to give typescript a try and want to implement all new modules in typescript. Just to see how it performs and how I like the idea. I do need to be able to revert back at any time so I do not want to put to much effort into the migration.

I started by using WebStorm and Gulp to support typescript. I created a new module and used the import / require combination.

import settings = require("./settings");
import _ = require("lodash-node");

By doing so I receive a couple of errors.

Error:(22, 27) TS2307: Cannot find external module './settings'.
Error:(23, 20) TS2307: Cannot find external module 'lodash-node'.

Those modules have no typescript definition file and are plain javascript modules (lodash is obviously a external library). I do have a couple of dependencies and I do not want to create definitions manually even if they are just empty stubs. As mentioned I do want to keep the integration as simple as possible.

Is there a general flag or something like that I can set?

Upvotes: 0

Views: 292

Answers (2)

stringparser
stringparser

Reputation: 745

You can use webpack's typescript-loader and start from there. Make sure you put target: 'node' on your webpack config so you don't have all the browser junk.

Upvotes: 0

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221104

The easiest way to proceed (if you don't want type information for a module) is to write:

declare module "lodash-node" {
  var notTyped: any; // naming is unimportant here
  export = notTyped;
}

Put that in a separate .d.ts file and /// <reference> it. Then _ will be of type any in your implementation file and the compiler won't complain about not know what module you're talking about.

Upvotes: 1

Related Questions