Typescript Design Pattern
Typescript Setup
npm init -y
npm install --save-dev typescript @types/node @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint
mkdir -p ./src && touch ./src/index.ts
echo 'console.log("Hello World!")' > ./src/index.ts
TypeScript Compiler
tsc --init
``./tsconfig.json`
{
"include": ["./src"],
"exclude": ["./node_modules", "./src/bak"],
"compilerOptions": {
"target": "ES2022",
"lib": ["DOM", "ES2022"],
"module": "ES2022",
"rootDir": "./",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "./public",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Add the following npm scripts to your ./package.json
file:
{
"name": "tspattern",
"version": "1.0.0",
"description": "",
"main": "./public/index.js",
"type": "module",
"scripts": {
"tsc": "tsc --watch",
"start": "node ./public/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"eslint": "^8.56.0",
"typescript": "^5.3.3"
}
}
And execute both scripts in two separate terminals:
npm run tsc
npm run start
> typescript-2023@1.0.0 start
> node ./src/index.js
Hello World!