mardi 24 novembre 2020

Best practices for using typescript with node

For react rthere is create-react-app which codifies best practices in project setup.

Something is missing in node. If I want to develop and deploy a typescript application on node.js I need at least 3 "targets":

  • start runs a development build of the app
  • test executes unittests (e.g. jest)
  • build compiles the typescript to something node can understand - for deployment.

For all this functionality there are tools like ts-jest ts-node and tsc.

This all somewhat works but i get all kinds of hickups. Currently the scripts section in package.json looks like this:

  "scripts": {
    "build": "tsc -p src",
    "start": "yarn run nodemon -e js,json,graphqls,ts,yaml --signal SIGHUP -x ts-node src/index.js",
    "test": "rm -Rf dist/ ; yarn run ts-jest --testPathIgnorePatterns=tests/",
  },

But this has all kinds of issues. tsc uses src/tsconfig.json. The other two use ./tsconfig.json. without that the output in ./dist/ is too deep nested for relative paths to work.

ts-node and tsc seem to report different errors (propbably a path thing). The contents of ./dist confuse the other tools etc.

Is there any guidance on how to set this up?

Aucun commentaire:

Enregistrer un commentaire