Using TypeScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is a transpiler, very much like Babel, but it's also a type checker. Type checking is a very useful especially when writing an NPM package or when dealing with big projects.

Setup

Install the dependencies.

$ npm install --save-dev typescript

Create a configuration file ./tsconfig.json.

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es3",
    "noImplicitAny": false,
    "sourceMap": false,
    "outDir": "dist",
    "declaration": true
  }
}

Replace .js file extension with .ts on all your ./src/*.js source files.

Configure the ./package.json.

{
  ...
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "scripts": {
    ...
    "clean": "rm -Rf ./dist",
    "build": "npm run clean; tsc",
    "prepublish": "npm run build"
  },
  ...
}

If you use nodemon --exec command, make sure you are watching also ts files and that you exclude the ./dist directory from the watch list in the nodemon.json file.

{
  ...
  "ignore": ["dist/*"],
  "ext": "js,ts"
}

results matching ""

    No results matching ""