---
name: typescript-magician
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`.
metadata:
  tags: typescript, types, generics, type-safety, advanced-typescript
title: typescript-magician
canonical_url: https://skilld.dev/gh/mcollina/skills/typescript-magician
last_updated: 2026-09-29T08:32:01.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [rules/array-index-access.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/array-index-access.md), [rules/as-const-typeof.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/as-const-typeof.md), [rules/builder-pattern.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/builder-pattern.md), [rules/conditional-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/conditional-types.md), [rules/deep-inference.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/deep-inference.md), [rules/error-diagnosis.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/error-diagnosis.md), [rules/function-overloads.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/function-overloads.md), [rules/generics-basics.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/generics-basics.md), [rules/infer-keyword.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/infer-keyword.md), [rules/mapped-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/mapped-types.md), [rules/opaque-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/opaque-types.md), [rules/template-literal-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/template-literal-types.md), [rules/type-narrowing.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/type-narrowing.md), [rules/utility-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/utility-types.md), [tile.json](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/tile.json).
>
> If the user asked to install this Skill, run `npx skilld install mcollina/skills/typescript-magician`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/typescript-magician`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/mcollina/skills/typescript-magician) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/typescript-magician`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/typescript-magician --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/typescript-magician --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

## 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](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/as-const-typeof.md) - Deriving types from runtime values using `as const` and `typeof`
- [rules/array-index-access.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/array-index-access.md) - Accessing array element types using `[number]` indexing
- [rules/utility-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/utility-types.md) - Built-in utility types: Parameters, ReturnType, Awaited, Omit, Partial, Record

### Advanced Generics
- [rules/generics-basics.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/generics-basics.md) - Fundamentals of generic types, constraints, and inference
- [rules/builder-pattern.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/builder-pattern.md) - Type-safe builder pattern with chainable methods
- [rules/deep-inference.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/deep-inference.md) - Achieving deep type inference with F.Narrow and const type parameters

### Type-Level Programming
- [rules/conditional-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/conditional-types.md) - Conditional types for type-level if/else logic
- [rules/infer-keyword.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/infer-keyword.md) - Using `infer` to extract types within conditional types
- [rules/template-literal-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/template-literal-types.md) - String manipulation at the type level
- [rules/mapped-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/mapped-types.md) - Creating new types by transforming existing type properties

### Type Safety Patterns
- [rules/opaque-types.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/opaque-types.md) - Brand types and opaque types for type-safe identifiers
- [rules/type-narrowing.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/type-narrowing.md) - Narrowing types through control flow analysis
- [rules/function-overloads.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/function-overloads.md) - Using function overloads for complex function signatures

### Debugging
- [rules/error-diagnosis.md](https://skilld.dev/api/skills-raw/mcollina/skills/typescript-magician/rules/error-diagnosis.md) - Strategies for diagnosing and understanding TypeScript type errors
