pihentagy / ts-hello-world

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hello world in typescript

  1. Make sure tsc is installed npm install -g typescript

  2. cd to an empty dir

  3. npm init -y (will create package.json)

  4. extend one of the @tsconfig/base configs in tsconfig.json

    tsconfig.json
    {
        "extends": "@tsconfig/node16/tsconfig.json",
        "include": ["src/**/*"], // (1)
        "exclude": ["node_modules", "**/*.spec.ts"],
        "compilerOptions": {
            "outDir": "./dist/" // (2)
        }
    }
    1. input files are in src

    2. Specifying output dir not to mix generated code with .ts files.

On the fly compilation and debugging

  1. install ts-node-dev

    npm i -D ts-node-dev+
  2. Add to package.json

    {
      "scripts": {
        "start": "tsnd --inspect=0.0.0.0:9229 --respawn --log-error ./src/app.ts",
      }
    }

Use includes with @ aliases

in dev with tsnd

  1. Install tsconfig-paths

    npm i -D tsconfig-paths
  2. When running tsnd, provide -r tsconfig-paths/register

    {
      "scripts": {
        "start-with-tsconfig-path": "tsnd --inspect=0.0.0.0:9229 --respawn --log-error -r tsconfig-paths/register ./src/app.ts",
      }
    }

in prod use tsc-alias

  1. npm i -D tsc-alias

  2. provide "baseUrl": "./src", in tsconfig.json

    {
      "scripts": {
        "build": "tsc; npx run tsc-alias",
        "prod": "npm run build && node ./dist/app.js"
      }
    }

About


Languages

Language:TypeScript 100.0%