Tips & Tricks

Claude Code के साथ Practical Tips for Accelerating TypeScript Development

Claude Code का उपयोग करके practical tips for accelerating typescript development सीखें। Practical tips और code examples शामिल हैं।

TypeScriptとClaude Codeは最高の組み合わせ

Claude CodeはTypeScriptの型システムを深く理解しており、type definitionsの自動generate से高度なgenericsのutilization तक幅広くサポートでき है।型errorのないrobustなcodeをfastにलिखना方法 introduceし है।

Tips 1:APIresponseの型を自動generate

APIのresponse सेtype definitionsを自動generateさせましょう。

> इसAPIendpointのresponseに基づいてtype definitionsをबनाओ。
> curl http://localhost:3000/api/users/1 の結果का उपयोग करके。
// Generated type definitions
interface User {
  id: string;
  email: string;
  profile: {
    firstName: string;
    lastName: string;
    avatar: string | null;
  };
  roles: Role[];
  createdAt: string;
  updatedAt: string;
}

interface Role {
  id: string;
  name: "admin" | "editor" | "viewer";
  permissions: string[];
}

interface ApiResponse<T> {
  data: T;
  meta: {
    requestId: string;
    timestamp: string;
  };
}

type UserResponse = ApiResponse<User>;
type UsersResponse = ApiResponse<User[]> & {
  meta: { page: number; total: number };
};

Tips 2:any型の撲滅

Project内のany型を一括でappropriateな型に変換でき है।

> Project内の any 型をसभीsearchして、
> appropriateな型に変換して。
> 変換बादに npx tsc --noEmit でtype checkを通して。

Tips 3:utility型のutilization

Claude Codeはappropriateなutility型を提案してくれ है।

> User型 सेupdate用の型を作って。
> id, createdAt, updatedAt は含めないで、全fieldをオプショナルに。
// Claude Codeがgenerateする型
type UpdateUserInput = Partial<Omit<User, "id" | "createdAt" | "updatedAt">>;

Tips 4:Zodスkeyマ से型を導出

validationスkeyマとtype definitionsの二重managementを解消し है।

> Zodスkeyマを定義して、वहां से型を導निकालनाるpatternに
> refactoringして。手動のtype definitionsはDelete。
import { z } from "zod";

const UserSchema = z.object({
  id: z.string().uuid(),
  email: z.string().email(),
  name: z.string().min(1).max(100),
  role: z.enum(["admin", "editor", "viewer"]),
  isActive: z.boolean().default(true),
});

// 型はスkeyマ से自動導出
type User = z.infer<typeof UserSchema>;

const CreateUserSchema = UserSchema.omit({ id: true });
type CreateUserInput = z.infer<typeof CreateUserSchema>;

Tips 5:型guardの自動generate

runtimeでのtype checkfunctionを自動generateさせ है।

> ApiError, NetworkError, ValidationError の
> 型guardfunctionをबनाओ。
interface ApiError {
  type: "api";
  statusCode: number;
  message: string;
}

interface NetworkError {
  type: "network";
  cause: Error;
}

interface ValidationError {
  type: "validation";
  fields: Record<string, string[]>;
}

type AppError = ApiError | NetworkError | ValidationError;

function isApiError(error: AppError): error is ApiError {
  return error.type === "api";
}

function isNetworkError(error: AppError): error is NetworkError {
  return error.type === "network";
}

function isValidationError(error: AppError): error is ValidationError {
  return error.type === "validation";
}

error型の設計patternके बारे मेंはerror handling設計patternमें विस्तार सेबताया गया है。

Tips 6:genericsのutilization

Claude Codeはcomplexなgenericsも正確に書け है।

> 型safeなeventエミッターをबनाओ。
> event名と引数の型を静的にcheckできる तरह。
type EventMap = {
  "user:login": { userId: string; timestamp: Date };
  "user:logout": { userId: string };
  "error": { message: string; code: number };
};

class TypedEventEmitter<T extends Record<string, unknown>> {
  private listeners = new Map<keyof T, Set<(data: never) => void>>();

  on<K extends keyof T>(event: K, listener: (data: T[K]) => void): void {
    if (!this.listeners.has(event)) {
      this.listeners.set(event, new Set());
    }
    this.listeners.get(event)!.add(listener as (data: never) => void);
  }

  emit<K extends keyof T>(event: K, data: T[K]): void {
    this.listeners.get(event)?.forEach(fn => fn(data as never));
  }
}

const emitter = new TypedEventEmitter<EventMap>();
emitter.on("user:login", (data) => {
  // data は { userId: string; timestamp: Date } と推論される
  console.log(data.userId);
});

CLAUDE.mdでTypeScriptruleをsettings

## TypeScriptrule
- strictモードを有効にする बात
- any型の使用は禁止
- 型アサーション(as)は最小限に
- utility型を積極的にutilization

生産性をइसके अलावाऊपरげるTipsは生産性を3倍にする10のTipsを、Reactdevelopmentでのutilization法はReactdevelopmentを爆速にする方法をदेखें。

Summary

Claude CodeはTypeScriptの型システムを深く理解しており、type definitionsの自動generate से高度なgenerics तक正確にsupportでき है।型safe性を維持しながらdevelopment速度を向ऊपरさせましょう。

TypeScriptके details के लिएTypeScriptofficial documentation、Claude Codeके बारे मेंはAnthropicofficial documentationをदेखें。

#Claude Code #TypeScript #type safety #development efficiency #frontend