Typescript Magician

作者 mcollina72b727514771無授權條款收錄於 2026年10月8日更新於 2026年10月8日

Designs complex generic types, refactors `any` types to strict alternatives, creates type guards and utility types, and resolves TypeScript compiler errors. Use when the user asks about TypeScript (TS) types, generics, type inference, type guards, removing `any` types, strict typing, type errors, `infer`, `extends`, conditional types, mapped types, template literal types, branded/opaque types, or utility types like `Partial`, `Record`, `ReturnType`, and `Awaited`.

AI 產生的概覽

指導 TypeScript 型別工作:泛型、條件型別與對應型別、型別守衛,以及移除 any 型別。

功能
此技能提供解決 TypeScript 型別問題的指令與參考規則。內容涵蓋進階泛型、條件型別、對應型別與樣板字面值型別、工具型別、品牌或不透明型別、型別縮窄、函式多載以及錯誤診斷。它產出型別安全的程式碼方案,包含修改前後範例、型別理論說明與型別測試,並指示代理在修改前後執行 tsc --noEmit。
適用情境
當使用者詢問 TypeScript 型別、泛型、型別推論、型別守衛、嚴格型別,或 infer、extends、條件型別、對應型別、工具型別等特定功能時使用。它也適合用來消除 any 型別與解決 TypeScript 編譯器錯誤。
執行需求
需要 TypeScript 專案以及可執行 tsc --noEmit 的 tsc 編譯器。不附帶指令碼,僅包含指令與 markdown 規則檔案。

When to use

Use this skill for:

  • TypeScript errors and type challenges
  • Eliminating any types from codebases
  • Complex generics and type inference issues
  • When strict typing is needed

Instructions

When invoked:

  1. Run tsc --noEmit to capture the full error output before making changes
  2. Identify the root cause of type issues (unsound inference, missing constraints, implicit any, etc.)
  3. Craft precise, type-safe solutions using advanced TypeScript features
  4. Eliminate all any types with proper typing — validate each replacement still satisfies call sites
  5. Confirm the fix compiles cleanly with a second tsc --noEmit pass

Capabilities include:

  • Advanced generics and conditional types
  • Template literal types and mapped types
  • Utility types and type manipulation
  • Brand types and nominal typing
  • Complex inference patterns
  • Variance and distribution rules
  • Module augmentation and declaration merging

For every TypeScript challenge:

  • Explain the type theory behind the problem
  • Provide multiple solution approaches when applicable
  • Show before/after type representations
  • Include comprehensive type tests
  • Ensure full IntelliSense support

Quick Examples

Eliminating any with generics

Before

ts
function getProperty(obj: any, key: string): any {  return obj[key];}

After

ts
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {  return obj[key];}// getProperty({ name: "Alice" }, "name") → inferred as string ✓

Narrowing an unknown API response

Before

ts
async function fetchUser(): Promise<any> {  const res = await fetch("/api/user");  return res.json();}

After

ts
interface User { id: number; name: string }
function isUser(value: unknown): value is User {  return (    typeof value === "object" &&    value !== null &&    "id" in value &&    "name" in value  );}
async function fetchUser(): Promise<User> {  const res = await fetch("/api/user");  const data: unknown = await res.json();  if (!isUser(data)) throw new Error("Invalid user shape");  return data;}

Reference

Read individual rule files for detailed explanations and code examples:

Core Patterns

  • rules/as-const-typeof.md [blocked] - Deriving types from runtime values using as const and typeof
  • rules/array-index-access.md [blocked] - Accessing array element types using [number] indexing
  • rules/utility-types.md [blocked] - Built-in utility types: Parameters, ReturnType, Awaited, Omit, Partial, Record

Advanced Generics

  • rules/generics-basics.md [blocked] - Fundamentals of generic types, constraints, and inference
  • rules/builder-pattern.md [blocked] - Type-safe builder pattern with chainable methods
  • rules/deep-inference.md [blocked] - Achieving deep type inference with F.Narrow and const type parameters

Type-Level Programming

  • rules/conditional-types.md [blocked] - Conditional types for type-level if/else logic
  • rules/infer-keyword.md [blocked] - Using infer to extract types within conditional types
  • rules/template-literal-types.md [blocked] - String manipulation at the type level
  • rules/mapped-types.md [blocked] - Creating new types by transforming existing type properties

Type Safety Patterns

  • rules/opaque-types.md [blocked] - Brand types and opaque types for type-safe identifiers
  • rules/type-narrowing.md [blocked] - Narrowing types through control flow analysis
  • rules/function-overloads.md [blocked] - Using function overloads for complex function signatures

Debugging

  • rules/error-diagnosis.md [blocked] - Strategies for diagnosing and understanding TypeScript type errors

來源與署名

來源:mcollina/skills位於skills/typescript-magician提交72b7275

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架