TypeScript Starter - A starter template with TypeScript rules, tooling, and configuration for Node.js projects.
// โ ENFORCED: Strict mode with comprehensive type checking // โ ENFORCED: No implicit any types // โ ENFORCED: No unused variables or parameters // โ ENFORCED: All code paths must return // โ ENFORCED: Explicit type annotations required // โ ENFORCED: No unreachable code // โ ENFORCED: Exact optional property types // โ ENFORCED: No unchecked indexed access // โ This will fail: function badFunction(input) { // implicit any if (condition) return value // not all paths return const unused = "never used" // unused variable } // โ This passes: function goodFunction: (input: string) => string = (input: string): string => { if (condition) { return value } return defaultValue }
// โ ENFORCED: Single quotes, no semicolons // โ ENFORCED: 2-space indentation // โ ENFORCED: No trailing commas // โ ENFORCED: Required curly braces // โ ENFORCED: Spaces inside object braces // โ ENFORCED: No spaces inside array brackets // โ ENFORCED: Consistent naming conventions // โ This will fail: const obj = {a: 1, b: 2,}; // trailing comma const arr = [ 1, 2, 3 ]; // spaces in array if(condition) return value; // no curly braces, semicolon // โ This passes: const obj = { a: 1, b: 2 } const arr = [1, 2, 3] if (condition) { return value }
// โ ENFORCED: Prefer const over let/var // โ ENFORCED: Arrow functions for callbacks // โ ENFORCED: Template literals over concatenation // โ ENFORCED: Object destructuring where applicable // โ ENFORCED: No inline comments // โ ENFORCED: Cognitive complexity โค 15 // โ ENFORCED: No duplicate code // โ ENFORCED: Prefer immediate returns // โ This will fail: var oldWay = "string" + variable; // var, concatenation function callback() { return value; } // function instead of arrow // inline comment // inline comment // โ This passes: const newWay = `string ${variable}` const callback = () => value // Block comment for complex logic
// โ ENFORCED: No duplicate strings // โ ENFORCED: No identical functions // โ ENFORCED: No redundant boolean expressions // โ ENFORCED: Prefer immediate returns // โ ENFORCED: No one-iteration loops // โ ENFORCED: No unused variables // โ ENFORCED: No unreachable code // โ This will fail: const message = "Hello" const greeting = "Hello" // duplicate string if (condition === true) { // redundant boolean return value } // โ This passes: const MESSAGES = { GREETING: "Hello" } as const if (condition) { return value }
# Clone and start coding git clone https://github.com/NeaByteLab/TypeScript-Starter.git my-project cd my-project npm install npm run dev
# Development npm run dev # TypeScript watch mode npm run build # Build for production npm run clean # Clean build directory # Code Quality npm run lint # ESLint with strict rules npm run lint:fix # Auto-fix ESLint violations npm run format # Prettier formatting npm run type-check # TypeScript type checking # Testing npm run test # Run tests with Jest npm run test:watch # Watch mode for TDD npm run test:coverage # Run tests with coverage # Documentation npm run docs # Generate TypeDoc documentation # Quality Assurance npm run check-all # Run all checks
TypeScript-Starter/
โโโ src/ # Source code
โ โโโ types/ # Type definitions
โ โ โโโ index.ts # Common types: ConfigOptions, Result<T>, EventHandler, etc.
โ โโโ utils/ # Utility functions
โ โ โโโ index.ts # Helper functions: isNotEmpty, createSuccessResult, etc.
โ โโโ index.ts # Main entry point - exports all public APIs
โโโ tests/ # Test files
โ โโโ setup.ts # Jest test setup and global configurations
โ โโโ example.test.ts # Test suite for all functions
โโโ examples/ # Example usage
โ โโโ basic-usage.ts # Usage examples with async support
โโโ dist/ # Build output (ESM + type definitions)
โโโ docs/ # Auto-generated TypeDoc documentation
โโโ coverage/ # Test coverage reports (HTML + LCOV)
โโโ Configuration Files # Strict rules enforced
โโโ tsconfig.json # TypeScript configuration (all strict options enabled)
โโโ eslint.config.js # ESLint configuration (200+ rules)
โโโ jest.config.js # Jest testing configuration with TypeScript
โโโ package.json # Dependencies and scripts
// Configuration options interface export interface ConfigOptions { readonly debug: boolean readonly timeout: number readonly retries: number } // Result type for operations (success/error handling) export type Result<T> = { readonly success: boolean readonly data?: T readonly error?: string } // Event handler type export type EventHandler<T = unknown> = (event: T) => void // Async operation type export type AsyncOperation<T> = () => Promise<T> // Validation function type export type Validator<T> = (value: T) => boolean
// String validation export const isNotEmpty: (value: string) => boolean // Result constructors export const createSuccessResult: <T>(data: T) => Result<T> export const createErrorResult: <T>(error: string) => Result<T> // Async utilities export const delay: (ms: number) => Promise<void>
// Example function (demonstrates strict typing) export const exampleFunction: (input: string) => string // Example class (demonstrates OOP with strict rules) export class ExampleClass { constructor(value: string) public getValue(): string public setValue(newValue: string): void }
import { exampleFunction, ExampleClass, isNotEmpty, createSuccessResult, createErrorResult, delay, type ConfigOptions, type Result } from 'typescript-starter' // Function usage const result = exampleFunction('hello world') console.log(result) // "HELLO WORLD" // Class usage const instance = new ExampleClass('test value') console.log(instance.getValue()) // "TEST VALUE" // Utility usage console.log(isNotEmpty('valid')) // true console.log(isNotEmpty('')) // false const success = createSuccessResult({ message: 'OK' }) const error = createErrorResult<string>('Failed') // Async usage await delay(1000) console.log('Delayed execution')
// โ Too complex (will fail) function complexFunction(input: string): string { if (condition1) { if (condition2) { if (condition3) { if (condition4) { if (condition5) { return "too complex" } } } } } return "default" } // โ Simple and clear (passes) function simpleFunction(input: string): string { if (!condition1) return "default" if (!condition2) return "default" if (!condition3) return "default" return "result" }
// โ These will fail compilation function badFunction(input) { // implicit any if (condition) return value // not all paths return const unused = "never used" // unused variable } // โ These pass strict checking function goodFunction(input: string): string { if (condition) { return value } return defaultValue }
// tests/example.test.ts import { exampleFunction, ExampleClass } from '@/index' describe('exampleFunction', () => { it('should convert string to uppercase', () => { const result = exampleFunction('test') expect(result).toBe('TEST') }) }) describe('ExampleClass', () => { let instance: ExampleClass beforeEach(() => { instance = new ExampleClass('initial') }) it('should set and get values correctly', () => { instance.setValue('new value') expect(instance.getValue()).toBe('NEW VALUE') }) })
- Lines: 80% minimum
- Functions: 100% for public APIs
- Branches: 80% minimum
- Statements: 80% minimum
# Run all tests npm test # Run tests in watch mode npm run test:watch # Run tests with coverage npm run test:coverage
/** * Example function demonstrating strict rules * @param input - Input string to process * @returns Processed string in uppercase */ export const exampleFunction: (input: string) => string = (input: string): string => { return input.toUpperCase() } /** * Example class with strict typing */ export class ExampleClass { private value: string constructor(initialValue: string) { this.value = initialValue.toUpperCase() } /** * Get the current value * @returns The current value in uppercase */ public getValue(): string { return this.value } /** * Set a new value * @param newValue - The new value to set */ public setValue(newValue: string): void { this.value = newValue.toUpperCase() } }
// โ Relative imports (messy) import { utils } from '../../../utils/helper' // โ Path aliases (clean) import { utils } from '@/utils/helper' import { types } from '@types/common' import { helpers } from '@utils/helpers'
@/*โsrc/*(main source directory)@types/*โsrc/types/*(type definitions)@utils/*โsrc/utils/*(utility functions)
npm run dev # Rules are checkednpm run test:watch # TDD approachnpm run check-all # All rules checkednpm run build # Production build with minification npm run docs # Generate TypeDoc documentation npm run clean # Clean build directory
MIT License - see LICENSE file for details.