developing-with-typescript
TypeScript 5.x development with type system, generics, utility types, and strict mode patterns. Use when writing TypeScript code or adding types to JavaScript projects.
What this skill does
# TypeScript Development Skill
TypeScript 5.x development with modern patterns including strict mode, generics, utility types, and modules.
**Progressive Disclosure**: Quick reference patterns here. See [REFERENCE.md](REFERENCE.md) for advanced topics.
---
## When to Use
Loaded by `backend-developer` or `frontend-developer` when:
- `tsconfig.json` present in project
- `package.json` contains `typescript` dependency
- `.ts` or `.tsx` files in project
---
## Quick Start
### Basic Types
```typescript
// Primitives
const name: string = "Alice";
const age: number = 30;
const isActive: boolean = true;
// Arrays and Tuples
const numbers: number[] = [1, 2, 3];
const point: [number, number] = [10, 20];
const rest: [string, ...number[]] = ["scores", 1, 2, 3];
```
### Interfaces vs Type Aliases
```typescript
// Interfaces - object shapes, extensible, declaration merging
interface User {
id: string;
name: string;
email: string;
}
interface Admin extends User {
permissions: string[];
}
// Type aliases - unions, tuples, primitives, complex types
type ID = string | number;
type Point = [number, number];
type Callback = (data: string) => void;
type AdminUser = User & { permissions: string[] };
```
### Functions
```typescript
// Basic function
function greet(name: string): string {
return `Hello, ${name}`;
}
// Arrow with optional/default params
const createUser = (name: string, age?: number, role = "user") => ({ name, age, role });
// Function overloads
function parse(input: string): string[];
function parse(input: string[]): string;
function parse(input: string | string[]): string | string[] {
return typeof input === "string" ? input.split(",") : input.join(",");
}
```
---
## Type System Essentials
### Union and Intersection Types
```typescript
// Union - one of multiple types
type Status = "pending" | "approved" | "rejected";
type Result = string | Error;
// Intersection - combine types
type Timestamped = { createdAt: Date; updatedAt: Date };
type Entity = User & Timestamped;
```
### Literal Types
```typescript
type Direction = "north" | "south" | "east" | "west";
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
// Template literal types
type EventName = `on${Capitalize<string>}`;
type Getter<T extends string> = `get${Capitalize<T>}`;
```
### Type Narrowing
```typescript
// typeof guard
function format(value: string | number): string {
return typeof value === "string" ? value.trim() : value.toFixed(2);
}
// in operator
function speak(animal: { bark(): void } | { meow(): void }): void {
if ("bark" in animal) animal.bark();
else animal.meow();
}
// Discriminated unions (recommended)
type Success = { status: "success"; data: string };
type Failure = { status: "failure"; error: Error };
type Result = Success | Failure;
function handle(result: Result): string {
return result.status === "success" ? result.data : result.error.message;
}
```
---
## Generics
### Basic Generics
```typescript
function identity<T>(value: T): T {
return value;
}
interface Box<T> {
value: T;
map<U>(fn: (value: T) => U): Box<U>;
}
class Container<T> {
constructor(private value: T) {}
get(): T { return this.value; }
}
```
### Constraints
```typescript
// extends constraint
function getLength<T extends { length: number }>(item: T): number {
return item.length;
}
// keyof constraint
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
// Default type
interface ApiResponse<T = unknown> {
data: T;
status: number;
}
```
---
## Utility Types
### Transformation
```typescript
interface User {
id: string;
name: string;
email: string;
age: number;
}
type PartialUser = Partial<User>; // All optional
type RequiredUser = Required<PartialUser>; // All required
type ReadonlyUser = Readonly<User>; // All readonly
type UserPreview = Pick<User, "id" | "name">;
type UserWithoutEmail = Omit<User, "email">;
type UserRoles = Record<string, "admin" | "user">;
```
### Extraction
```typescript
// Extract/Exclude from unions
type Numbers = Extract<string | number | boolean, number>; // number
type NotNumber = Exclude<string | number | boolean, number>; // string | boolean
// Remove null/undefined
type Defined = NonNullable<string | null | undefined>; // string
// Function types
type Return = ReturnType<typeof createUser>;
type Params = Parameters<typeof createUser>;
// Unwrap Promise
type Unwrapped = Awaited<Promise<string>>; // string
```
---
## tsconfig.json Essentials
### Recommended Strict Config
```json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"declaration": true,
"outDir": "./dist",
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
```
### Key Strict Flags
| Flag | Purpose |
|------|---------|
| `strict` | Enable all strict checks |
| `noImplicitAny` | Error on implicit any |
| `strictNullChecks` | null/undefined require handling |
| `noUncheckedIndexedAccess` | Index access may be undefined |
---
## Module Patterns
### Import/Export
```typescript
// Named exports
export const PI = 3.14159;
export function calculate(r: number): number { return PI * r ** 2; }
export interface Circle { radius: number; }
// Default export
export default class Calculator { }
// Re-exports
export { User } from "./user";
export * from "./utils";
// Type-only imports
import type { User } from "./types";
export type { Config } from "./config";
```
### Declaration Files
```typescript
// types.d.ts
declare module "untyped-library" {
export function process(input: string): string;
}
// Extend existing module
declare module "express" {
interface Request { userId?: string; }
}
// Global declarations
declare global {
interface Window { myApp: { version: string }; }
}
```
---
## Common Patterns
### Type Guards
```typescript
// User-defined type guard
function isString(value: unknown): value is string {
return typeof value === "string";
}
function isUser(obj: unknown): obj is User {
return typeof obj === "object" && obj !== null && "id" in obj && "name" in obj;
}
// Assertion function
function assertDefined<T>(value: T | undefined): asserts value is T {
if (value === undefined) throw new Error("Value is undefined");
}
```
### Branded Types
```typescript
// Prevent type confusion
type UserId = string & { readonly brand: unique symbol };
type OrderId = string & { readonly brand: unique symbol };
function createUserId(id: string): UserId { return id as UserId; }
function createOrderId(id: string): OrderId { return id as OrderId; }
function getUser(id: UserId): User { /* ... */ }
const userId = createUserId("user-123");
getUser(userId); // OK
// getUser(createOrderId("order-456")); // Error!
```
### Result Type
```typescript
type Result<T, E = Error> =
| { success: true; data: T }
| { success: false; error: E };
function divide(a: number, b: number): Result<number, string> {
if (b === 0) return { success: false, error: "Division by zero" };
return { success: true, data: a / b };
}
const result = divide(10, 2);
if (result.success) console.log(result.data);
else console.error(result.error);
```
---
## Quick Reference
### Assertions
```typescript
const value = someValue as string; // Type assertion
const element = document.getElementById("app")!; // Non-null assertion
const config = { api: "/api" } as const; // Const assertion
```
### Index Signatures
```typescript
interface StringMap { [key: string]: string; }
interface NumberMap { [index: number]: string; }
interface DataAttrs { [key: `data-${string}`]: string; }
```
### Mapped Types
```typescript
type Optional<T> = { [K in keyof T]?: T[K] };
type Immutable<T> = { readonly [K in keyof T]: T[KRelated in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.