---
name: write-advanced-typescript-types
description: Applies TypeScript generics, utility types, mapped types and conditional types so values stay precise instead of any. Use when the user wants a generic helper, a mapped or conditional type, or to replace a wide type with a precise one.
license: MIT
metadata:
  category: frontend
  tags: "typescript,generics"
  author: johnny-bravo
  version: "1.0"
  updated: "2026-11-07"
  status: published
  aliases: "use-advanced-typescript-types, mapped types, conditional types, utility types"
  source_url: "https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/javascript-typescript/skills/typescript-advanced-types"
  source_license: MIT
  copyright: "Copyright (c) 2024 Seth Hobson"
  original_author: "Seth Hobson (wshobson/agents, typescript-advanced-types)"
  modified: "Adapted by TheSkillVault: rewritten as a short order of work (narrow types, generics, utilities, mapped and conditional types) with original snippets; dropped the long type catalogue; added gotchas and done-when."
  imported_on: "2026-11-07"
---

# Write advanced TypeScript types

Reach for a clever type only when a plain interface cannot say the rule. The goal is that an illegal value fails at compile time, and the call site still reads clearly.

## When to use
- A function returns `any` or a wide `string`, so callers lose the real type.
- The same shape is repeated with every field optional, or with every field renamed.
- Two modes of a value are optional fields that can be mixed into nonsense.

## When not to use
- You want a review score for encapsulation of a domain class. That is a review prompt, not a typing guide.
- The project is plain JavaScript and there is no plan to add a compiler.

## Steps
1. Write the data as a small interface or a union of string literals first. Do not start with a conditional type.
2. If a function should hand back the same type it received, give it a generic. Constrain it with `extends` so a wrong argument fails.
3. Use a utility type for a one-step change: `Pick`, `Omit`, `Partial`, `Record`. Prefer those over a hand-written copy.
4. Use a mapped type when every key changes in the same way, such as renaming each field into a getter name.
5. Use a conditional type when the output depends on the input, such as unwrapping an array. Add `infer` only when you must name the inner type.
6. Model modes as a discriminated union (`kind: "a" | "b"`) instead of a pile of optional fields.
7. Check the call sites. If the caller needs `as` or `any` to compile, the type is wrong. Fix the type, not the caller.
8. Export the helper with one example in a comment or a test. A type nobody can call is not done.

```ts
type Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] };
type Unwrap<T> = T extends (infer U)[] ? U : T;
function first<T>(items: readonly T[]): T | undefined {
  return items[0];
}
```

## Gotchas
- `any` switches the checker off for everything it touches. Use `unknown` and narrow it.
- A conditional type over a naked type parameter distributes across unions. That surprises people. Wrap the check in a tuple (`[T] extends [Something]`) when you do not want that.
- Do not paste production records, tokens or personal data into type examples.
- Very deep types slow the editor and hide errors. Stop at one mapped type plus one conditional. If you need more, change the data shape.

## Portability
This is TypeScript. The same patterns exist in other typed languages: generics, and sum types instead of optional fields. If you use JSDoc in plain JavaScript, you can write a shorter form of the same signatures. If you use Python, use dataclasses or TypedDict rather than these TS helpers.

## Done when
- Call sites compile without `any` or a type assertion.
- Repeated shapes go through a utility, mapped or conditional type instead of a copy.
- Modes that cannot happen together are a union, not optional fields.

## Example prompt
"This function takes an object and returns the same keys with get added. Write the mapped type and a generic signature, and do not use any."

---
Adapted from "typescript-advanced-types" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.
