home / skills / skillcreatorai / ai-agent-skills / javascript-typescript

javascript-typescript skill

/skills/javascript-typescript

This skill assists with TypeScript configuration, type patterns, modern JavaScript, React and Node.js patterns to speed full-stack development.

npx playbooks add skill skillcreatorai/ai-agent-skills --skill javascript-typescript

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

Files (1)
SKILL.md
3.1 KB
---
name: javascript-typescript
description: JavaScript and TypeScript development with ES6+, Node.js, React, and modern web frameworks. Use for frontend, backend, or full-stack JavaScript/TypeScript projects.
source: wshobson/agents
license: MIT
---

# JavaScript/TypeScript Development

## TypeScript Configuration

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "skipLibCheck": true,
    "declaration": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}
```

## Type Patterns

### Utility Types
```typescript
// Pick specific properties
type UserPreview = Pick<User, 'id' | 'name'>;

// Omit properties
type CreateUser = Omit<User, 'id' | 'createdAt'>;

// Make all properties optional
type PartialUser = Partial<User>;

// Make all properties required
type RequiredUser = Required<User>;

// Extract union types
type Status = 'pending' | 'active' | 'inactive';
type ActiveStatus = Extract<Status, 'active' | 'pending'>;
```

### Discriminated Unions
```typescript
type Result<T> =
  | { success: true; data: T }
  | { success: false; error: Error };

function handleResult<T>(result: Result<T>) {
  if (result.success) {
    console.log(result.data); // T
  } else {
    console.error(result.error); // Error
  }
}
```

### Generic Constraints
```typescript
interface HasId {
  id: string | number;
}

function findById<T extends HasId>(items: T[], id: T['id']): T | undefined {
  return items.find(item => item.id === id);
}
```

## Modern JavaScript

### Destructuring & Spread
```javascript
const { name, ...rest } = user;
const merged = { ...defaults, ...options };
const [first, ...others] = items;
```

### Optional Chaining & Nullish Coalescing
```javascript
const city = user?.address?.city ?? 'Unknown';
const count = data?.items?.length ?? 0;
```

### Array Methods
```javascript
const adults = users.filter(u => u.age >= 18);
const names = users.map(u => u.name);
const total = items.reduce((sum, item) => sum + item.price, 0);
const hasAdmin = users.some(u => u.role === 'admin');
const allActive = users.every(u => u.active);
```

## React Patterns

```typescript
// Props with children
interface CardProps {
  title: string;
  children: React.ReactNode;
}

// Event handlers
interface ButtonProps {
  onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
}

// Custom hooks
function useLocalStorage<T>(key: string, initial: T) {
  const [value, setValue] = useState<T>(() => {
    const stored = localStorage.getItem(key);
    return stored ? JSON.parse(stored) : initial;
  });

  useEffect(() => {
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);

  return [value, setValue] as const;
}
```

## Node.js Patterns

```typescript
// ES Modules
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

// Error handling
process.on('unhandledRejection', (reason) => {
  console.error('Unhandled Rejection:', reason);
  process.exit(1);
});
```

Overview

This skill provides practical JavaScript and TypeScript development guidance, patterns, and configurations for ES6+, Node.js, React, and modern web frameworks. It focuses on robust TypeScript setup, common type patterns, modern JS idioms, React component and hook patterns, and Node.js module and error-handling conventions. Use it to accelerate front-end, back-end, and full-stack workflows with clear, production-ready examples.

How this skill works

It inspects typical project needs and supplies concrete patterns: a recommended TypeScript compiler configuration, utility types, discriminated unions, generics, and common ES2020+ idioms. It demonstrates React prop typing, custom hook structure, and Node.js ES module usage and runtime error handling. Use the examples directly in your codebase or adapt them to enforce stricter typing, predictable runtime behavior, and clearer component APIs.

When to use it

  • Starting a new TypeScript project and needing a sensible tsconfig baseline
  • Migrating JavaScript code to TypeScript to add type safety incrementally
  • Designing typed React components, hooks, and reusable UI primitives
  • Implementing backend services with Node.js and ES module patterns
  • Standardizing utility types and common patterns across a monorepo

Best practices

  • Enable strict compiler options and declaration outputs for library code
  • Prefer discriminated unions for error/success flows and clear type narrowing
  • Use utility types (Pick, Omit, Partial, Required) to avoid duplicating interfaces
  • Favor ES module imports, top-level async handling, and explicit process-level error logging in Node
  • Keep hooks generic and return tuples with as const to preserve tuple types

Example use cases

  • Create a shared types package with Pick/Omit utilities and exported declaration files
  • Implement a Result<T> pattern to unify API responses and simplify error handling
  • Write a reusable useLocalStorage<T> hook with proper generic typing for UI state persistence
  • Build a Node.js CLI or microservice using ES modules, node:fs/promises, and graceful unhandledRejection handling
  • Migrate a React component library to stricter TypeScript settings and generate .d.ts for consumers

FAQ

What TypeScript target and module settings are recommended?

Target a modern runtime like ES2022 and use ESNext modules with bundler-style resolution for contemporary toolchains.

How do I choose between Partial and Pick/Omit?

Use Partial when making every property optional; use Pick/Omit to transform specific properties without losing the original interface shape.