I am new to TypeScript and am investigating migrating a large, existing ES2017-compliant NodeJS codebase to it.
Here's what my tsconfig.json
looks like:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./built",
"allowJs": true,
"target": "ES2017",
},
"include": [
"**/*"
],
"exclude": [
"**/node_modules/**/*",
"built"
]
}
If I rename all of my .js
files to have .ts
extensions, I experience this issue and see lots of Cannot redeclare block-scoped variable
errors from tsc
because TypeScript seemingly can't tell whether my files are modules (each with their own scope) or scripts (all share one scope).
These "dummy"/rote errors prevent me from seeing other, real errors that need to be fixed as part of the TypeScript migration.
So, my question is, what's the most sensible and correct way to resolve this issue as part of migrating an existing JavaScript codebase to TypeScript?
I thought of two options:
Leave
allowJs
enabled and slowly rename each.js
file to.ts
one at a time, tweaking each file'simports
/exports
at the time each file is renamed, to indicate to TypeScript that the file is a module and won't conflict with other.ts
files. (Existing.js
files would just be copied over unaffected.)Rename all
.js
files to.ts
and put a cannedexport {};
in every single file as a starting point. This doesn't seem ideal.
Neither of these options seems ideal. Is there a more straightforward way to tell tsc
to treat all files as modules to prevent it from thinking that they will all share the same scope?