Skip to content
Back to skills

Ts Generics

ASecurity

Improve TypeScript generics and inference so call sites stay type-safe without manual annotations

  • 3 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 3, 2026
ai-agentstypescriptgoapi

Works with

  • api

Security analysis

A100/100

Scanned September 3, 2026

npx -y skills add black141312/ada --skill ts-generics --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Ts Generics?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Ts Generics
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/black141312-ts-generics/badge)](https://www.skillsdirectory.com/skills/black141312-ts-generics)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: ts-generics
description: Improve TypeScript generics and inference so call sites stay type-safe without manual annotations
category: languages
---

# Ts Generics

Reach for this when an API forces callers to annotate types manually, returns `any`, or loses the relationship between input and output types.

1. Identify where types are flowing through: a function/class that takes a value and should return a type derived from it is a generics candidate.
2. Introduce type parameters that get inferred from arguments (`function get<T>(obj: T, key: keyof T)`) so callers never pass `<...>` explicitly.
3. Constrain parameters with `extends` (`<T extends Record<string, unknown>>`) to enable property access and reject bad inputs at the call site.
4. Use conditional and mapped types (`T extends U ? A : B`, `{ [K in keyof T]: ... }`) and `infer` to transform types instead of widening to `any`.
5. Preserve literal types where they matter with `const` type parameters (`<const T>`) or `as const`, so unions don't collapse to `string`/`number`.
6. Verify inference with type-level tests (`expectTypeError` / `// @ts-expect-error`) and by hovering real call sites — confirm no caller needs explicit args.

## Rules
- A generic that appears only once in a signature is usually pointless — each type parameter should connect at least two positions (input↔output).
- Constrain before you access: you can't read `t.id` off an unconstrained `T` — add `extends { id: ... }`.
- Don't over-engineer; if a plain union or overload is clearer than a conditional type, use it.
- Keep error messages legible — deeply nested conditional types produce inscrutable errors; name intermediate types as aliases.
- Default type parameters (`<T = unknown>`) for ergonomics, but never default to `any`.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.