I have a javascript node application that is quiet big now and I want to switch to typescript to accelerate the development and better maintain the code. I installed typescript with node and mocha types with the commands
npm i typescript
npm i @types/node
npm i @types/mocha
I use a tsconfig.json file which is as follows:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"files": [
"./node_modules/@types/mocha/index.d.ts",
"./node_modules/@types/node/index.d.ts"
],
"include": [
"src/**/*.ts",
],
"exclude": [
"node_modules"
]
}
I added the "tsc": "tsc" script in the package.json to compile the code using the npm run tsc command.
Here is my project architecture:
- dist // created at compile time
- src
--- server.ts
--- configs
----- config.js
--- // other subdirectories
- package.json
- tsconfig.json
When I compile the code, it creates a dist folder that only contains the compiled server.ts file, the only file I renamed to .ts. So when I launch the compiled app with node dist/server.js all the other files are missing and the app crashes.
My question is: do I have to switch the whole application to typescript, renaming every file to .ts and changing imports or is there a way I can do it gradually and keep some javascript files?
allowJsoption and other advice discussed in the Migrating from JavaScript documentation