{"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.","category":"frontend","tags":["typescript","generics"],"models":["any"],"proven":false,"passing_families":0,"model_testing":"Not yet model-tested; testing Jan 2027","url":"https://theskillvault.ai/skills/write-advanced-typescript-types","api_url":"https://theskillvault.ai/api/skills/write-advanced-typescript-types","raw_url":"https://theskillvault.ai/api/skills/write-advanced-typescript-types/raw","body":"# Write advanced TypeScript types\n\nReach 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.\n\n## When to use\n- A function returns `any` or a wide `string`, so callers lose the real type.\n- The same shape is repeated with every field optional, or with every field renamed.\n- Two modes of a value are optional fields that can be mixed into nonsense.\n\n## When not to use\n- You want a review score for encapsulation of a domain class. That is a review prompt, not a typing guide.\n- The project is plain JavaScript and there is no plan to add a compiler.\n\n## Steps\n1. Write the data as a small interface or a union of string literals first. Do not start with a conditional type.\n2. If a function should hand back the same type it received, give it a generic. Constrain it with `extends` so a wrong argument fails.\n3. Use a utility type for a one-step change: `Pick`, `Omit`, `Partial`, `Record`. Prefer those over a hand-written copy.\n4. Use a mapped type when every key changes in the same way, such as renaming each field into a getter name.\n5. 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.\n6. Model modes as a discriminated union (`kind: \"a\" | \"b\"`) instead of a pile of optional fields.\n7. Check the call sites. If the caller needs `as` or `any` to compile, the type is wrong. Fix the type, not the caller.\n8. Export the helper with one example in a comment or a test. A type nobody can call is not done.\n\n```ts\ntype Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] };\ntype Unwrap<T> = T extends (infer U)[] ? U : T;\nfunction first<T>(items: readonly T[]): T | undefined {\n  return items[0];\n}\n```\n\n## Gotchas\n- `any` switches the checker off for everything it touches. Use `unknown` and narrow it.\n- 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.\n- Do not paste production records, tokens or personal data into type examples.\n- 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.\n\n## Portability\nThis 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.\n\n## Done when\n- Call sites compile without `any` or a type assertion.\n- Repeated shapes go through a utility, mapped or conditional type instead of a copy.\n- Modes that cannot happen together are a union, not optional fields.\n\n## Example prompt\n\"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.\"\n\n---\nAdapted from \"typescript-advanced-types\" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.","files":["LICENSE.txt"],"license":"MIT","license_name":"MIT License","license_url":"https://opensource.org/license/mit","attribution":"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","install":{"claude_code":"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":"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'","agents":"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'","mcp_url":"https://theskillvault.ai/mcp","cursor_mcp_deeplink":"cursor://anysphere.cursor-deeplink/mcp/install?name=theskillvault&config=eyJ1cmwiOiJodHRwczovL3RoZXNraWxsdmF1bHQuYWkvbWNwIn0%3D","mcp_config":{"mcpServers":{"theskillvault":{"url":"https://theskillvault.ai/mcp"}}}},"bundles":[],"tested_on":[{"family":"chatgpt","name":"ChatGPT","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"claude","name":"Claude","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"gemini","name":"Gemini","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"grok","name":"Grok","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"llama","name":"Llama","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"deepseek","name":"DeepSeek","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"mistral","name":"Mistral","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"qwen","name":"Qwen","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"kimi","name":"Kimi K","outcome":null,"date":null,"source":null,"counts":false,"stale":false}],"proven_rule":{"threshold":5,"window_days":365,"computed_at":"2026-11-29T03:42:30.374Z"}}