home / skills / alinaqi / claude-bootstrap / typescript

typescript skill

/skills/typescript

This skill enforces rigorous TypeScript strict mode and eslint-driven quality for reliable, secure project initialization and maintainable code.

npx playbooks add skill alinaqi/claude-bootstrap --skill typescript

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.9 KB
---
name: typescript
description: TypeScript strict mode with eslint and jest
---

# TypeScript Skill

*Load with: base.md*

---

## Strict Mode (Non-Negotiable)

```json
// tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}
```

---

## Project Structure

```
project/
├── src/
│   ├── core/               # Pure business logic
│   │   ├── types.ts        # Domain types/interfaces
│   │   ├── services/       # Pure functions
│   │   └── index.ts        # Public API
│   ├── infra/              # Side effects
│   │   ├── api/            # HTTP handlers
│   │   ├── db/             # Database operations
│   │   └── external/       # Third-party integrations
│   └── utils/              # Shared utilities
├── tests/
│   ├── unit/
│   └── integration/
├── package.json
├── tsconfig.json
└── CLAUDE.md
```

---

## Tooling (Required)

```json
// package.json scripts
{
  "scripts": {
    "lint": "eslint src/ --ext .ts,.tsx",
    "typecheck": "tsc --noEmit",
    "test": "jest",
    "test:coverage": "jest --coverage",
    "format": "prettier --write 'src/**/*.ts'"
  }
}
```

```javascript
// eslint.config.js
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
  eslint.configs.recommended,
  ...tseslint.configs.strictTypeChecked,
  {
    rules: {
      '@typescript-eslint/no-explicit-any': 'error',
      '@typescript-eslint/explicit-function-return-type': 'error',
      'max-lines-per-function': ['error', 20],
      'max-depth': ['error', 2],
      'max-params': ['error', 3],
    }
  }
);
```

---

## Testing with Jest

```typescript
// tests/unit/services/user.test.ts
import { calculateTotal } from '../../../src/core/services/pricing';

describe('calculateTotal', () => {
  it('returns sum of item prices', () => {
    // Arrange
    const items = [{ price: 10 }, { price: 20 }];

    // Act
    const result = calculateTotal(items);

    // Assert
    expect(result).toBe(30);
  });

  it('returns zero for empty array', () => {
    expect(calculateTotal([])).toBe(0);
  });

  it('throws on invalid item', () => {
    expect(() => calculateTotal([{ invalid: 'item' }])).toThrow();
  });
});
```

---

## GitHub Actions

```yaml
name: TypeScript Quality Gate

on: [push, pull_request]

jobs:
  quality:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          
      - name: Install dependencies
        run: npm ci
        
      - name: Lint
        run: npm run lint
        
      - name: Type Check
        run: npm run typecheck
        
      - name: Test with Coverage
        run: npm run test:coverage
        
      - name: Coverage Threshold (80%)
        run: npm run test:coverage -- --coverageThreshold='{"global":{"branches":80,"functions":80,"lines":80,"statements":80}}'
```

---

## Pre-Commit Hooks

Using Husky + lint-staged:

```bash
npm install -D husky lint-staged
npx husky init
```

```json
// package.json
{
  "lint-staged": {
    "*.{ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ]
  }
}
```

```bash
# .husky/pre-commit
npx lint-staged
npx tsc --noEmit
npm run test -- --onlyChanged --passWithNoTests
```

This runs on every commit:
1. ESLint + Prettier on staged files
2. Type check entire project
3. Tests for changed files only

---

## Type Patterns

### Discriminated Unions for Results
```typescript
type Result<T> =
  | { ok: true; value: T }
  | { ok: false; error: string };

function parseUser(data: unknown): Result<User> {
  // Type-safe error handling without exceptions
}
```

### Branded Types for IDs
```typescript
type UserId = string & { readonly brand: unique symbol };
type OrderId = string & { readonly brand: unique symbol };

// Can't accidentally pass UserId where OrderId expected
function getOrder(orderId: OrderId): Order { ... }
```

### Const Assertions for Literals
```typescript
const STATUSES = ['pending', 'active', 'closed'] as const;
type Status = typeof STATUSES[number]; // 'pending' | 'active' | 'closed'
```

### Zod for Runtime Validation
```typescript
import { z } from 'zod';

const UserSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1).max(100),
});

type User = z.infer<typeof UserSchema>;
```

---

## TypeScript Anti-Patterns

- ❌ `any` type - use `unknown` and narrow
- ❌ Type assertions (`as`) - use type guards
- ❌ Non-null assertions (`!`) - handle null explicitly
- ❌ `@ts-ignore` without explanation
- ❌ Enums - use const objects or union types
- ❌ Classes for data - use interfaces/types
- ❌ Default exports - use named exports

Overview

This skill provides an opinionated TypeScript starter tailored for strict mode projects with ESLint and Jest integrated. It enforces strong type safety, consistent project layout, and automated quality checks across CI and pre-commit hooks. The configuration aims for secure, maintainable code and predictable developer experience.

How this skill works

It enables TypeScript strict compiler options and a strict ESLint configuration to catch type and style issues early. The package scripts and GitHub Actions run linting, type checks, and Jest tests (with coverage enforcement) on pushes and pull requests. Pre-commit hooks run linting, formatting, type checks, and changed-file tests to prevent low-quality code entering the repo.

When to use it

  • Starting a new TypeScript project that must prioritize correctness and security.
  • Bootstrapping services or libraries where API contracts and runtime validation are important.
  • Teams that require automated CI checks and pre-commit enforcement to maintain quality.
  • Codebases that benefit from discriminated unions, branded types, and runtime validation patterns.
  • Projects that want a consistent structure separating pure business logic from side effects.

Best practices

  • Enable full TypeScript strict mode and fail the build on type errors.
  • Separate core business logic (pure functions) from infra/side-effect code for easier testing.
  • Use discriminated unions and branded types to model domain-safe identifiers and results.
  • Prefer runtime validation libraries like Zod for parsing external input instead of trusting types alone.
  • Enforce ESLint rules that ban explicit any, excessive complexity, and implicit returns.
  • Run lint, typecheck, and tests in CI and as pre-commit hooks to catch regressions early.

Example use cases

  • API service where src/core contains business rules and src/infra contains HTTP and DB adapters.
  • Monorepo package that must export a stable, well-typed public API from src/core/index.ts.
  • Secure form handling using Zod schemas to validate and infer types for request payloads.
  • Library development with strict lint rules and tests to keep public surface minimal and predictable.
  • Continuous integration pipelines that block merges with failing type checks, lint errors, or low coverage.

FAQ

How do I enforce strict compiler checks locally and in CI?

Run tsc --noEmit locally and include npm run typecheck as a CI step; pre-commit hooks should also run tsc --noEmit to block commits with type errors.

When should I use branded types versus plain strings?

Use branded types for domain identifiers to prevent accidental mixing of different ID types at compile time; use plain strings only when there is no cross-domain risk.