Typescript migration headaches
I had the pleasure of migrating an existing codebase from straight nodejs to Typescript. Here’s a few of the headaches/gotchas I ran into.
Extending from Error breaks things
The following custom error class resulted in an error complaining about
Mocha not picking up sub directories with –recursive
With a test structure like:
mocha --recursive test/**/*.spec.tswill only see mocha pickup tests in
app.spec.ts, despite the
--recursiveoption being set.
As ScottFreeCode points out the solution is to quote the glob to avoid having the shell interpret the **.
mocha --recursive "test/**/*.spec.ts"
Typescript compiler will not touch non-ts/js files
.jsonschemas to the
distdirectory. This instead needs to be done separately.
With only a few files to copy I ended up creating a simple bin script to copy the files before running
Sequelize unexpected token in map file error
A fairly common pattern to follow when using Sequelize is to load all model files defined in a particular directory, with code like:
Unexpected tokenwith Typescript when map file generation is turned on. Any code loading files should be modified to explicitly check the extension. In this case, we want to exclude any
*.map.jsfiles from being loaded.
More details can be found on this TypeScript github issue.
Cannot redeclare block-scoped variable
With a codebase split up amongst a number of files, Typescript instantly complained about
Cannot redeclare block-scoped variablewhenever attempting to load a local file.
The solution is to ensure each file exported every function/variable used in other files.