AGENT SKILL
typescript-magician
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`.
适用职业开发工程师
- License 许可
- MIT
- 最近更新
- 2026-06-24
- Stars
- ★ 66
- Forks
- 24
01 前言 Front Matter
- name
- typescript-magician
- metadata
- { "tags": "typescript, types, generics, type-safety, advanced-typescript" }
- description
- 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`.
02 资源文件 35
- 其他customize.toml1.6 KB
- 资产文件data/domain-complexity.csv7.2 KB
- 参考文档data/prd-purpose.md7.1 KB
- 资产文件data/project-types.csv3.1 KB
- 参考文档rules/array-index-access.md4.8 KB
- 参考文档rules/as-const-typeof.md4.6 KB
- 参考文档rules/builder-pattern.md8.0 KB
- 参考文档rules/conditional-types.md6.9 KB
- 参考文档rules/deep-inference.md6.7 KB
- 参考文档rules/error-diagnosis.md7.7 KB
- 参考文档rules/function-overloads.md7.4 KB
- 参考文档rules/generics-basics.md6.9 KB
- 参考文档rules/infer-keyword.md7.1 KB
- 参考文档rules/mapped-types.md7.3 KB
- 参考文档rules/opaque-types.md6.9 KB
- 参考文档rules/template-literal-types.md7.3 KB
- 参考文档rules/type-narrowing.md8.5 KB
- 参考文档rules/utility-types.md6.5 KB
- 参考文档steps-c/step-01-init.md9.5 KB
- 参考文档steps-c/step-01b-continue.md5.2 KB
- 参考文档steps-c/step-02-discovery.md8.2 KB
- 参考文档steps-c/step-02b-vision.md6.9 KB
- 参考文档steps-c/step-02c-executive-summary.md7.3 KB
- 参考文档steps-c/step-03-success.md9.2 KB
- 参考文档steps-c/step-04-journeys.md9.1 KB
- 参考文档steps-c/step-05-domain.md7.0 KB
- 参考文档steps-c/step-06-innovation.md8.7 KB
- 参考文档steps-c/step-07-project-type.md8.9 KB
- 参考文档steps-c/step-08-scoping.md11.4 KB
- 参考文档steps-c/step-09-functional.md9.2 KB
- 参考文档steps-c/step-10-nonfunctional.md9.0 KB
- 参考文档steps-c/step-11-polish.md9.6 KB
- 参考文档steps-c/step-12-complete.md5.3 KB
- 参考文档templates/prd-template.md164 B
- 参考文档tile.json651 B
03 SKILL.md 正文
When to use
Use this skill for:
- TypeScript errors and type challenges
- Eliminating
anytypes from codebases - Complex generics and type inference issues
- When strict typing is needed
Instructions
When invoked:
- Run
tsc --noEmitto capture the full error output before making changes - Identify the root cause of type issues (unsound inference, missing constraints, implicit
any, etc.) - Craft precise, type-safe solutions using advanced TypeScript features
- Eliminate all
anytypes with proper typing — validate each replacement still satisfies call sites - Confirm the fix compiles cleanly with a second
tsc --noEmitpass
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
function getProperty(obj: any, key: string): any {
return obj[key];
}
After
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
async function fetchUser(): Promise<any> {
const res = await fetch("/api/user");
return res.json();
}
After
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 - Deriving types from runtime values using
as constandtypeof - rules/array-index-access.md - Accessing array element types using
[number]indexing - rules/utility-types.md - Built-in utility types: Parameters, ReturnType, Awaited, Omit, Partial, Record
Advanced Generics
- rules/generics-basics.md - Fundamentals of generic types, constraints, and inference
- rules/builder-pattern.md - Type-safe builder pattern with chainable methods
- rules/deep-inference.md - Achieving deep type inference with F.Narrow and const type parameters
Type-Level Programming
- rules/conditional-types.md - Conditional types for type-level if/else logic
- rules/infer-keyword.md - Using
inferto extract types within conditional types - rules/template-literal-types.md - String manipulation at the type level
- rules/mapped-types.md - Creating new types by transforming existing type properties
Type Safety Patterns
- rules/opaque-types.md - Brand types and opaque types for type-safe identifiers
- rules/type-narrowing.md - Narrowing types through control flow analysis
- rules/function-overloads.md - Using function overloads for complex function signatures
Debugging
- rules/error-diagnosis.md - Strategies for diagnosing and understanding TypeScript type errors