Starter Templates

October 13, 2025 ยท View on GitHub

This directory contains optional starter templates to help you get started quickly with common TypeScript patterns.

๐ŸŽฏ Usage

These are example starting points - feel free to use them as-is, modify them, or delete them entirely and create your own structure.

๐Ÿ“ Available Templates

Basic Application

A simple TypeScript application structure:

// src/index.ts
import { App } from './app';

async function main(): Promise<void> {
  const app = new App();
  await app.start();
}

main().catch((error) => {
  console.error('Fatal error:', error);
  process.exit(1);
});

CLI Application

For command-line tools:

// src/cli.ts
import { parseArgs } from 'node:util';

function main(): void {
  const { values, positionals } = parseArgs({
    options: {
      help: { type: 'boolean', short: 'h' },
      version: { type: 'boolean', short: 'v' }
    },
    allowPositionals: true
  });

  if (values.help) {
    console.log('Usage: my-cli [options] <command>');
    return;
  }

  // Your CLI logic here
}

main();

Express Server

For web applications:

// src/server.ts
import express from 'express';

const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());

app.get('/', (req, res) => {
  res.json({ message: 'Hello, World!' });
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

Library/Package

For creating reusable libraries:

// src/index.ts

/**
 * Main library export
 */
export class MyLibrary {
  /**
   * Example method
   */
  public doSomething(): string {
    return 'Hello from MyLibrary';
  }
}

// Export types
export type { MyOptions } from './types';

// Export utilities
export { myUtilityFunction } from './utils';

๐Ÿ”„ Replacing Example Code

The template currently includes a logging system example. To replace it:

Option 1: Keep the Structure, Replace Logic

# Keep the directory structure but replace the logic in:
# - src/core/action.ts
# - src/index.ts

Option 2: Start Completely Fresh

# Remove all example code
rm -rf src/*

# Create your entry point
touch src/index.ts

# Add your first file
cat > src/index.ts << 'EOF'
console.log('Hello from my new project!');
EOF

Option 3: Keep Useful Parts

# Keep the logging system
mv src/logging /tmp/logging

# Clean everything else
rm -rf src/*

# Restore logging
mv /tmp/logging src/logging

# Create your new entry point
touch src/index.ts

๐Ÿ“š Pattern Examples

Dependency Injection

// src/container.ts
export class Container {
  private services = new Map<string, any>();

  register<T>(name: string, service: T): void {
    this.services.set(name, service);
  }

  resolve<T>(name: string): T {
    return this.services.get(name);
  }
}

Configuration Management

// src/config.ts
import { z } from 'zod';

const ConfigSchema = z.object({
  port: z.number().default(3000),
  env: z.enum(['development', 'production']).default('development'),
  database: z.object({
    host: z.string(),
    port: z.number()
  })
});

export type Config = z.infer<typeof ConfigSchema>;

export function loadConfig(): Config {
  return ConfigSchema.parse({
    port: Number(process.env.PORT),
    env: process.env.NODE_ENV,
    database: {
      host: process.env.DB_HOST,
      port: Number(process.env.DB_PORT)
    }
  });
}

Error Handling

// src/errors.ts
export class AppError extends Error {
  constructor(
    message: string,
    public code: string,
    public statusCode: number = 500
  ) {
    super(message);
    this.name = 'AppError';
  }
}

export class ValidationError extends AppError {
  constructor(message: string) {
    super(message, 'VALIDATION_ERROR', 400);
    this.name = 'ValidationError';
  }
}

๐Ÿ’ก Tips

  1. Start Simple: Begin with a basic structure and add complexity as needed
  2. Test Early: Write tests as you build features
  3. Follow Patterns: Maintain consistency in your code organization
  4. Document: Add JSDoc comments to public APIs
  5. Refactor: Continuously improve your code structure

๐ŸŽ“ Learning Resources