Migrate a JavaScript Nodejs project to TypeScript

  1. rename .js files to .ts files
  2. refactor import and export. Prefer named exports, avoid using default export.
  3. (for nodejs) if you put db object on the Request, use declaration merging to add db to the Request. Good article here: https://dev.to/kwabenberko/extend-express-s-request-object-with-typescript-declaration-merging-1nn5
  4. if you use a npm module that don’t have a type defined, you can use following method to make the type error goes away:
for frontend tsconfig.json:
{
"compilerOptions": {
"paths": {
"*": [
"node_modules/*",
"src/typings/*"
]
}
}
}
for nodejs tsconfig.json:
{
"compilerOptions": {
"typeRoots": [
"src/types",
"./node_modules/@types"
],
"baseUrl": "./src",
}
}

then do something like this:

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Can’t Access Camera or Microphone In WebRTC Apps? Solve Using “navigator.mediaDevices” Web API

Declare Missing Helper Function (TypeScript) in Visual Studio Code

Kiss my CSS

Handy Method for Measuring Execution Time. (C# and Javascript)

Top 10 Date Pickers in React

First Impressions: React Context API

Enabling ODP.NET Perfmon counters for Managed Provider

How to set up a React app with a Flask and MongoDB backend using Docker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Steve Mu

Steve Mu

Software Engineer

More from Medium

About Node.js

NodeJS & HTTP Error 431

Why use Brackets TypeORM in MySQL