├── .eslintignore ├── .eslintrc ├── .gitignore ├── .prettierrc ├── .vscode ├── launch.json └── tasks.json ├── README.md ├── build └── index.js ├── jest.config.js ├── nodemon.json ├── package-lock.json ├── package.json ├── src ├── index.spec.ts ├── index.ts └── useCase │ ├── example.feature │ ├── example.spec.ts │ └── example.ts └── tsconfig.json /.eslintignore: -------------------------------------------------------------------------------- 1 | # don't ever lint node_modules 2 | node_modules 3 | # don't lint build output (make sure it's set to your correct build folder name) 4 | build 5 | # don't lint nyc coverage output 6 | coverage 7 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "parser": "@typescript-eslint/parser", 4 | "plugins": ["@typescript-eslint", "prettier", "jest"], 5 | "extends": [ 6 | "eslint:recommended", 7 | "plugin:@typescript-eslint/recommended", 8 | "prettier" 9 | ], 10 | "rules": { 11 | "no-console": 1, 12 | "prettier/prettier": 2 13 | }, 14 | "env": { 15 | "browser": true, 16 | "node": true, 17 | "jest/globals": true 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and not Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | # Stores VSCode versions used for testing VSCode extensions 107 | .vscode-test 108 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "all", 3 | "singleQuote": true, 4 | "printWidth": 80 5 | } 6 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "node", 6 | "request": "launch", 7 | "name": "Debug TypeScript in Node.js", 8 | "preLaunchTask": "npm: build", 9 | "program": "${workspaceFolder}/src/index.ts", 10 | "protocol": "inspector", 11 | "outFiles": ["${workspaceFolder}/build/**/*.js"], 12 | "sourceMaps": true, 13 | "smartStep": true, 14 | "internalConsoleOptions": "openOnSessionStart" 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "type": "typescript", 8 | "tsconfig": "simple-typescript-starter/tsconfig.json", 9 | "problemMatcher": ["$tsc"] 10 | } 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🧰 Simple TypeScript Starter | 2024 2 | 3 | > We talk about a lot of **advanced Node.js and TypeScript** concepts on [the blog](https://khalilstemmler.com), particularly focused around Domain-Driven Design and large-scale enterprise application patterns. However, I received a few emails from readers that were interested in seeing what a basic TypeScript starter project looks like. So I've put together just that. 4 | 5 | ### Features 6 | 7 | - Minimal 8 | - TypeScript v4 9 | - Testing with Jest 10 | - Linting with Eslint and Prettier 11 | - Pre-commit hooks with Husky 12 | - VS Code debugger scripts 13 | - Local development with Nodemon 14 | 15 | ### Scripts 16 | 17 | #### `npm run start:dev` 18 | 19 | Starts the application in development using `nodemon` and `ts-node` to do hot reloading. 20 | 21 | #### `npm run start` 22 | 23 | Starts the app in production by first building the project with `npm run build`, and then executing the compiled JavaScript at `build/index.js`. 24 | 25 | #### `npm run build` 26 | 27 | Builds the app at `build`, cleaning the folder first. 28 | 29 | #### `npm run test` 30 | 31 | Runs the `jest` tests once. 32 | 33 | #### `npm run test:dev` 34 | 35 | Run the `jest` tests in watch mode, waiting for file changes. 36 | 37 | #### `npm run prettier-format` 38 | 39 | Format your code. 40 | 41 | #### `npm run prettier-watch` 42 | 43 | Format your code in watch mode, waiting for file changes. 44 | -------------------------------------------------------------------------------- /build/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | // eslint-disable-next-line no-console 3 | console.log('Hello world!'); 4 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | transform: { 3 | '^.+\\.ts?$': 'ts-jest', 4 | }, 5 | testEnvironment: 'node', 6 | testRegex: './src/.*\\.(test|spec)?\\.(ts|ts)$', 7 | moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], 8 | roots: ['/src'], 9 | }; 10 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": ".ts,.js", 4 | "ignore": [], 5 | "exec": "npx ts-node ./src/index.ts" 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-starter", 3 | "version": "1.0.0", 4 | "description": "A basic typescript app starter for 2024.", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "rimraf ./build && tsc", 8 | "start:dev": "npx nodemon", 9 | "start": "npm run build && node build/index.js", 10 | "lint": "eslint . --ext .ts", 11 | "prettier-format": "run-script-os", 12 | "prettier-format:win32": "prettier --config .prettierrc \"./src/**/*.ts\" --write", 13 | "prettier-format:darwin:linux": "prettier --config .prettierrc 'src/**/*.ts' --write", 14 | "prettier-format:default": "prettier --config .prettierrc 'src/**/*.ts' --write", 15 | "prettier-watch": "run-script-os", 16 | "prettier-watch:win32": "onchange \"src/**/*.ts\" -- prettier --write {{changed}}", 17 | "prettier-watch:darwin:linux": "onchange 'src/**/*.ts' -- prettier --write {{changed}}", 18 | "prettier-watch:default": "onchange 'src/**/*.ts' -- prettier --write {{changed}}", 19 | "test": "jest", 20 | "test:dev": "jest --watchAll" 21 | }, 22 | "husky": { 23 | "hooks": { 24 | "pre-commit": "npm run test && npm run prettier-format && npm run lint" 25 | } 26 | }, 27 | "keywords": [], 28 | "author": "", 29 | "license": "ISC", 30 | "devDependencies": { 31 | "@types/jest": "^28.1.8", 32 | "@types/node": "^18.6.1", 33 | "@typescript-eslint/eslint-plugin": "^5.31.0", 34 | "@typescript-eslint/parser": "^5.31.0", 35 | "eslint": "^8.20.0", 36 | "eslint-config-prettier": "^8.5.0", 37 | "eslint-plugin-jest": "^26.6.0", 38 | "eslint-plugin-prettier": "^4.2.1", 39 | "husky": "^8.0.1", 40 | "jest": "^28.1.3", 41 | "nodemon": "^2.0.19", 42 | "onchange": "^7.1.0", 43 | "prettier": "^2.7.1", 44 | "rimraf": "^3.0.2", 45 | "run-script-os": "^1.1.6", 46 | "ts-jest": "^28.0.7", 47 | "ts-node": "^10.9.1", 48 | "typescript": "^4.7.4" 49 | }, 50 | "dependencies": { 51 | "jest-cucumber": "^3.0.1" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/index.spec.ts: -------------------------------------------------------------------------------- 1 | describe('test', () => { 2 | test('add', async () => { 3 | expect(1 + 1).toEqual(2); 4 | }); 5 | }); 6 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | // eslint-disable-next-line no-console 2 | console.log('Hello world!'); 3 | -------------------------------------------------------------------------------- /src/useCase/example.feature: -------------------------------------------------------------------------------- 1 | Feature: Guesser 2 | 3 | Scenario: Guessing correctly 4 | Given the Guesser was assigned the word "silky" 5 | When a correct guess is made for "silky" 6 | Then the Guesser should update the number of correct guesses to 1 -------------------------------------------------------------------------------- /src/useCase/example.spec.ts: -------------------------------------------------------------------------------- 1 | 2 | import { Guesser } from './example' 3 | 4 | import { defineFeature, loadFeature } from 'jest-cucumber'; 5 | import path from 'path'; 6 | 7 | const feature = loadFeature(path.join(__dirname, './example.feature')); 8 | 9 | defineFeature(feature, test => { 10 | test('Guessing correctly', ({ given, when, then }) => { 11 | let guesser = new Guesser(); 12 | 13 | given(/^the Guesser was assigned the word "(.*)"$/, (arg0) => { 14 | guesser.setWord(arg0); 15 | }); 16 | 17 | when(/^a correct guess is made for "(.*)"$/, (arg0) => { 18 | guesser.guessWord(arg0) 19 | }); 20 | 21 | then(/^the Guesser should update the number of correct guesses to (\d+)$/, (arg0) => { 22 | expect(guesser.getNumberCorrectGuesses()).toEqual(Number(arg0)); 23 | }); 24 | }); 25 | }); 26 | 27 | -------------------------------------------------------------------------------- /src/useCase/example.ts: -------------------------------------------------------------------------------- 1 | 2 | export class Guesser { 3 | private word: string = ''; 4 | private correctGuesses = 0; 5 | 6 | public setWord (word: string) { 7 | this.word = word; 8 | } 9 | 10 | public guessWord (word: string): boolean { 11 | let correct = word.toLowerCase() === this.word.toLowerCase(); 12 | if (correct) this.correctGuesses++; 13 | return correct; 14 | } 15 | 16 | public getNumberCorrectGuesses (): number { 17 | return this.correctGuesses; 18 | } 19 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "lib": ["es6"], 6 | "allowJs": true, 7 | "outDir": "build", 8 | "rootDir": "src", 9 | "strict": true, 10 | "esModuleInterop": true, 11 | "types": ["node", "jest"], 12 | "skipLibCheck": true 13 | }, 14 | "include": ["src/**/*"], 15 | "exclude": ["src/**/*.spec.ts"] 16 | } 17 | --------------------------------------------------------------------------------