Migrate a JavaScript Nodejs project to TypeScript
- if using create-react-app, eject it so you can edit the tsconfig.json freely. It also allows index.ts to behave correctly when you use “export * from ‘./component”.
- rename .js files to .ts files
- refactor import and export. Prefer named exports, avoid using default export.
- (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
- 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:
