8

It seems that just installing it and then changing file extensions to .ts is not enough to effectively move to TypeScript with a NodeJS project. I get about 400 errors that seem to stem mostly from module resolution issues (but not only).

There are a couple of guides covering this topic, but they do not seem to help, maybe because they are one year old at best. Therefor, I want to ask for recent advice.

What steps do I need to take to transition a NodeJS project to Typescript?

3 Answers 3

7

1.Add typings to your devDependencies in package.json

{
  "devDependencies": {
    "typings": "latest"
  },
  "scripts": {
    "postinstall": "typings install --save"
  }
}

2.Add typings.json (alongside your package.json), note postinstall action in the package.json above - this will install typescript definitions upon each npm install

{
  "globalDependencies": {
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts"
  }
}

3.Add tsconfig.json

{ 
     "compilerOptions": { 
         "emitDecoratorMetadata": true, 
         "experimentalDecorators": true,
         "moduleResolution": "node",
         "module": "commonjs", 
         "target": "es6",
         "sourceMap": true,
         "outDir": "dist",
         "declaration": true,
         "allowJs": true,
         "forceConsistentCasingInFileNames": true
     },
     "exclude": [
         "node_modules",
         "dist",
         ".vscode",
         "docs"
     ]
} 

Note that allowJs option will help you to keep some javascript files unconverted if necessary.

  1. Make transpiling of ts files part of your build process

This should get you going. After that step by step convert javascript to typescript in order to leverage its benefits.

Hope this helps.

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

7 Comments

Thank you for these detailed steps, it has been hard to know which config options to choose. However, I did try setting allowJs to true to do it incrementally (and add a build directory where the output should go), but then, the ts compiler complained that it cannot compile because it would overwrite the files in the build directory. But I did add the build directory to the excludes, just like node_modules, so I did not know what to do and just changed all files to .ts. Did I miss something there?
You did not forget to specify outDir in tsconfig? Also this might be connected with how your build is setup, are you using gulp/grunt or compiling directly via tsc. I have used both and both work just fine with allowJs option. I would suggest you to try and reduce your project to minimum in order to clearly see where the problem will disappear.
I did eventually get it to work today morning, by specifying both "exclude" (build, node_modules) and "include" (src, where I moved my code files)(and yes, outDir="build"). This surprised me a lot, but without it (ie. with only "exclude" or "include" I was getting those errors and tsc was setting up the file structure inside build in a very weird way.
Also, with your specific tsconfig.json I get the error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
Quite strange, I do use this two options together without any issues. Although you are correct and there is open issue: github.com/Microsoft/TypeScript/issues/7546. Thanks for the info though.
|
5

Migrating Js to TS

You create a tsconfig.json with allowJs set to true. This allows you to use the .js as it is!. Next you start changing the file extension from .ts to .js one by one, declaring what you find missing as you go along.

I did a video about this about a week ago https://www.youtube.com/watch?v=gmKXXI_ck7w

More :

maybe because they are one year old at best.

I keep this up to date : https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

Of course if you read it as a book I assume you already have seen the nodejs quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

3 Comments

Thank you for maintaining that guide. I did try setting allowJs to true to do it incrementally (and add a build directory where the output should go), but then, the ts compiler complained that it cannot compile because it would overwrite the files in the build directory. But I did add the build directory to the excludes, just like node_modules, so I did not know what to do and just changed all files to .ts. Did I miss something there?
Please also use outDir. This is shown in the video as well :)
I did use it then, but I recently got it working by adding both "include" and "exclude" (and outDir is still there). Weird that I had to use both, but oh well
4

There also official documentation on how to do this

https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html

Comments

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.