Woodshop
Search…
V8
In this example, we will guide you through the steps to turn your project into a TypeScript project. After the guide has been completed, you will benefit from strong type checking and code completion.
Use this example only for version 8

Requirements

In this example, we will assume that you are familiar with TypeScript and that you have it installed. If this is not the case, simply run the following to get packages you will need in this Woodshop: npm install --save-dev typescript tsc-watch
To make it easy for you, we created our own typings to help you using our exposed tools using TypeScript. Until version 6 they were located in the DefintelyTyped GitHub repository. Starting from version 7, they are now directly integrated into our forest-express-sequelize and forest-express-mongoose packages. That being said, no additional package needs to be installed.

Configuration

As for every TypeScript project, we need to create a configuration file. It helps the transpiler to know where to take the files from, and where to transpile them. To have a smooth migration into TypeScript, we will use the following configuration:
Add this file at the root of your project.
SQL
MongoDB
tsconfig.json
1
{
2
"compilerOptions": {
3
"module": "commonjs",
4
"moduleResolution": "node",
5
"pretty": true,
6
"sourceMap": false,
7
"target": "es2017",
8
"outDir": "./dist",
9
"baseUrl": "./",
10
"types" : ["node", "express", "forest-express-sequelize", "sequelize"],
11
"allowJs": true
12
},
13
"include": ["./**/*", ".env"],
14
"exclude": ["node_modules", "dist"]
15
}
Copied!
tsconfig.json
1
{
2
"compilerOptions": {
3
"module": "commonjs",
4
"moduleResolution": "node",
5
"pretty": true,
6
"sourceMap": false,
7
"target": "es2017",
8
"outDir": "./dist",
9
"baseUrl": "./",
10
"types" : ["node", "express", "forest-express-mongoose", "mongoose"],
11
"allowJs": true
12
},
13
"include": ["./**/*", ".env"],
14
"exclude": ["node_modules", "dist"]
15
}
Copied!
This tells the transpiler to take every file using .ts or .js (see allowJs option) as an extension, and to transpile them into a ./dist folder. This is where your transpiled files will be. This type of configuration allows you to translate your app into TypeScript in dribs and drabs. No need to translate every file at once, change only the files you are interested in. Now that your TypeScript transpiler is set, we need to update our package.json with new scripts to fit with the new structure of our project.
package.json
1
"scripts": {
2
"start": "node ./dist/server.js",
3
"build": "rm -rf ./dist && npm run compile && npm run cp-config",
4
"dev": "tsc-watch --onSuccess \"node ./dist/server.js\"",
5
"compile": "tsc",
6
"lint": "eslint . -c .eslintrc.json --ext .ts",
7
"cp-config": "cp .env ./dist/"
8
},
Copied!
Note that the script dev is the script to use while developing. It will transpile your sources every time you perform a change, and it will refresh your server. Finally, let's install the mandatory typings for a newly generated project, run this from a command prompt:
SQL
MongoDB
1
npm install --save-dev @types/node @types/express @types/sequelize
Copied!
1
npm install --save-dev @types/node @types/express @types/mongoose
Copied!
And that's it! You are now able to code using TypeScript in your app. Simply change the extension from .js to .ts , change some code, and your file will be automatically handled.
The following section of this documentation will guide you through the migration of your files from .js to .ts. If you are using Mongoose, please refer to the Migrate Mongoose files section, otherwise, you are using Sequelize, and then please refer to the Migrate Sequelize files section.
Last modified 1mo ago
Copy link