TypeScript/JavaScript Coding Style

This file extends common/coding-style.md with TypeScript/JavaScript specific content.

Immutability

Use spread operator for immutable updates:

1// WRONG: Mutation
2function updateUser(user, name) {
3  user.name = name  // MUTATION!
4  return user
5}
6
7// CORRECT: Immutability
8function updateUser(user, name) {
9  return {
10    ...user,
11    name
12  }
13}

Error Handling

Use async/await with try-catch:

1try {
2  const result = await riskyOperation()
3  return result
4} catch (error) {
5  console.error('Operation failed:', error)
6  throw new Error('Detailed user-friendly message')
7}

Input Validation

Use Zod for schema-based validation:

1import { z } from 'zod'
2
3const schema = z.object({
4  email: z.string().email(),
5  age: z.number().int().min(0).max(150)
6})
7
8const validated = schema.parse(input)

Console.log

  • No console.log statements in production code
  • Use proper logging libraries instead
  • See hooks for automatic detection