Frontend · Skill
Write advanced TypeScript types
write-advanced-typescript-types
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.
Works with: Any AI Not yet model-tested; testing Jan 2027
Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works
Install in Claude Code, Cursor or Codex (optional)
Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.
Claude Code (all your projects)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-advanced-typescript-types/raw' -o ~/'.claude/skills/write-advanced-typescript-types/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-advanced-typescript-types/files/LICENSE.txt' -o ~/'.claude/skills/write-advanced-typescript-types/LICENSE.txt'Cursor (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-advanced-typescript-types/raw' -o '.cursor/skills/write-advanced-typescript-types/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-advanced-typescript-types/files/LICENSE.txt' -o '.cursor/skills/write-advanced-typescript-types/LICENSE.txt'Codex and other Agent Skills tools (this project)
curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-advanced-typescript-types/raw' -o '.agents/skills/write-advanced-typescript-types/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/write-advanced-typescript-types/files/LICENSE.txt' -o '.agents/skills/write-advanced-typescript-types/LICENSE.txt'
Or connect the whole library over MCP
Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):
{
"mcpServers": {
"theskillvault": {
"url": "https://theskillvault.ai/mcp"
}
}
}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
anyor a widestring, 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
- Write the data as a small interface or a union of string literals first. Do not start with a conditional type.
- If a function should hand back the same type it received, give it a generic. Constrain it with
extendsso a wrong argument fails. - Use a utility type for a one-step change:
Pick,Omit,Partial,Record. Prefer those over a hand-written copy. - Use a mapped type when every key changes in the same way, such as renaming each field into a getter name.
- Use a conditional type when the output depends on the input, such as unwrapping an array. Add
inferonly when you must name the inner type. - Model modes as a discriminated union (
kind: "a" | "b") instead of a pile of optional fields. - Check the call sites. If the caller needs
asoranyto compile, the type is wrong. Fix the type, not the caller. - Export the helper with one example in a comment or a test. A type nobody can call is not done.
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
anyswitches the checker off for everything it touches. Useunknownand 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
anyor 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.
Bundled files
Licence
Adapted from Seth Hobson (wshobson/agents, typescript-advanced-types) (Copyright (c) 2024 Seth Hobson), MIT License. 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.
Licence: MIT License. Attribution when you reuse it: Based on work by Seth Hobson (wshobson/agents, typescript-advanced-types) (Copyright (c) 2024 Seth Hobson, MIT License), https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/javascript-typescript/skills/typescript-advanced-types; adapted by TheSkillVault, https://theskillvault.ai