TypeScript

TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

TypeScript Official Website

Installation

Install the TypeScript configuration using the ServerCN CLI:

npx servercn-cli add tooling typescript

Configuration

The component provides a tsconfig.json file optimized for modern Node.js backend development.

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2021",
    "module": "es2022",
    "moduleResolution": "bundler",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "dist",
    "sourceMap": true,
    "rootDir": "src",
    "alwaysStrict": true,
    "useUnknownInCatchVariables": true,
    "forceConsistentCasingInFileNames": true,
    "paths": {
      "@/*": [ "./src/*" ]
    }
  },
  "include": [ "src/**/*" ],
  "exclude": [ "node_modules" ]
}
{
  "compilerOptions": {
    "target": "ES2021",
    "module": "es2022",
    "moduleResolution": "bundler",
    "lib": ["ES2022"],
    "allowJs": true,
    "checkJs": false,
    "declaration": true,
    "sourceMap": true,
    "outDir": "dist",
    "rootDir": "src",
    "removeComments": true,
 
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "useUnknownInCatchVariables": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
 
    "paths": {
      "@/*": ["./src/*"]
    },
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
 
    "skipLibCheck": true,
    "resolveJsonModule": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "coverage", "**/*.test.ts"]
}

Scripts

Add the following scripts to your package.json:

{
  "scripts": {
    "dev": "tsx watch src/server.ts",
    "build": "tsc",
    "start": "node dist/server.js",
    "typecheck": "tsc --noEmit"
  }
}

Using Nodemon (Optional)

If you prefer nodemon, add a nodemon.json file:

{
  "watch": ["src"],
  "ext": "ts",
  "exec": "tsx src/server.ts"
}

Then update your scripts:

{
  "scripts": {
    "dev": "nodemon",
    "build": "tsc",
    "start": "node dist/server.js",
    "typecheck": "tsc --noEmit"
  }
}

Features

  • Strict Type Checking: Enabled by default to catch bugs early.
  • Modern ES Support: Pre-configured for the latest ECMAScript features.
  • Source Maps: Configured for easier debugging.
  • Clean Output: Separates source code from compiled JavaScript.

Add More Tooling

npx servercn-cli add tooling commitlint husky eslint prettier lint-staged typescript 

Installation

npx servercn-cli add tooling typescript