🎯

generics-as-functions

🎯Skill

from marius-townhouse/effective-typescript-skills

VibeIndex|
What it does

Constrains and transforms types like functions, using generics to create flexible, reusable type utilities with clear input restrictions.

πŸ“¦

Part of

marius-townhouse/effective-typescript-skills(83 items)

generics-as-functions

Installation

Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills --all
Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills -s prefer-unknown-over-any exhaustiveness-checking
Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills -a opencode claude-code
Quick InstallInstall with npx
npx skills add marius-townhouse/effective-typescript-skills -l
git cloneClone repository
git clone https://github.com/marius-townhouse/effective-typescript-skills.git
πŸ“– Extracted from docs: marius-townhouse/effective-typescript-skills
1Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

Use when defining generic types or functions. Use when constraining type parameters. Use when writing type-level code. Use when documenting generic types.

Overview

# Think of Generics as Functions Between Types

Overview

Generic types are the type-level equivalent of functions in value space. Just as a function takes parameters and returns a value, a generic type takes type parameters and produces a concrete type. This mental model helps you write better generic types by applying the same principles you use for writing functions: constraining inputs, choosing good names, and documenting behavior.

Understanding generics as functions between types clarifies when to use constraints, how to name type parameters, and why some generic patterns work while others don't. This perspective is essential for effective type-level programming in TypeScript.

When to Use This Skill

  • Defining generic types that transform other types
  • Writing generic functions with type parameters
  • Constraining what types can be passed to generics
  • Documenting generic types with TSDoc
  • Creating reusable type utilities

The Iron Rule

Think of generic types as functions between types: use extends to constrain inputs like type annotations, choose descriptive names, and document with @template TSDoc.

Detection

Watch for these patterns:

```typescript

// RED FLAGS - Poor generic design

type MyPick = { [P in K]: T[P] }; // No constraints, errors in implementation

type BadGeneric = ...; // Single-letter names without context

function parse(input: string): T; // Return-only generic, no better than any

```

Generic Types as Functions

A generic type takes type parameters and produces a concrete type:

```typescript

// Generic type "function"

type MyPartial = { [K in keyof T]?: T[K] };

// "Calling" the function with Person

type PartPerson = MyPartial;

// Equivalent to: { name?: string; age?: number; }

```

Just like functions, generic types can have multiple parameters:

```typescript

// Two type parameters

type MyPick = { [P in K]: T[P] };

// Usage

type NameOnly = MyPick;

// Equivalent to: { name: string }

```

Constraining Type Parameters

Use extends to constrain type parameters, just as you'd use type annotations for function parameters:

```typescript

// GOOD: Constrained type parameters

type MyPick = {

[P in K]: T[P]

};

// Without constraints - allows invalid instantiations

type BadPick = { [P in K]: T[P] }; // Errors in implementation

// Invalid uses caught by constraints:

type Bad1 = MyPick; // Error: 'firstName' not in Person

type Bad2 = MyPick<'age', Person>; // Error: string doesn't satisfy object

```

Naming Type Parameters

Choose descriptive names, especially for complex generics:

```typescript

// Short names OK for simple, local generics

type Partial = { [K in keyof T]?: T[K] };

// Longer names for complex or exported generics

type MapValues<

ObjectType extends object,

ValueTransformer extends (value: any) => any

> = {

[Key in keyof ObjectType]: ValueTransformer

};

```

Documenting Generics

Use @template TSDoc tag to document type parameters:

```typescript

/**

* Construct a new object type using a subset of properties from another.

* @template T - The original object type

* @template K - The keys to pick, typically a union of string literal types

*/

type MyPick = {

[P in K]: T[P]

};

```

Generic Functions

Generic functions define associated generic types and enable type inference:

```typescript

function pick(

obj: T,

...keys: K[]

): Pick {

const result: Partial> = {};

for (const k of keys) {

result[k] = obj[k];

}

return result as Pick;

}

// TypeScript infers types from arguments

const person = { name: 'Alice', age: 30 };

const nameOnly = pick(person, 'name');

// Type: Pick<{ name: string; age: number }, 'name'>

```

Generic Classes

Generic classes capture types that don't need to be passed to methods:

```typescript

class Box {

value: T;

constructor(value: T) {

this.value = value;

}

getValue(): T {

return this.value;

}

}

// Type inferred from constructor

const dateBox = new Box(new Date());

// Type: Box

```

Pressure Resistance Protocol

When pressured to use unconstrained generics:

  1. Add constraints: Use extends to limit valid type arguments
  2. Consider defaults: Provide sensible defaults for type parameters
  3. Document requirements: Use TSDoc to explain constraints
  4. Test edge cases: Verify generics work with unions and edge cases

Red Flags

| Anti-Pattern | Why It's Bad |

|--------------|--------------|

| Unconstrained type parameters | Allows invalid instantiations, implementation errors |

| Single-letter names in complex generics | Reduces readability |

| Return-only generics | Equivalent to type assertions, no type safety |

| Missing TSDoc on public generics | Poor developer experience |

Common Rationalizations

"Constraints limit flexibility"

Reality: Constraints catch errors at the type level rather than producing confusing type errors or wrong types. They document valid usage.

"T, K, V are standard names"

Reality: They are conventional for simple cases, but descriptive names improve readability in complex generics. Match name length to scope.

"Users can figure out the types"

Reality: Documentation helps users understand generics without reading implementation. @template tags appear in IDE tooltips.

Quick Reference

| Concept | Value-Level | Type-Level |

|---------|-------------|------------|

| Definition | function | type |

| Parameters | (x: T) | |

| Return | : ReturnType | = ResultType |

| Documentation | @param | @template |

| Constraints | Type annotations | extends keyword |

The Bottom Line

Generic types are functions between types. Apply the same principles you use for writing functions: constrain inputs, choose meaningful names, and document thoroughly. This mental model makes complex type-level code more approachable and maintainable.

Reference

  • Effective TypeScript, 2nd Edition by Dan Vanderkam
  • Item 50: Think of Generics as Functions Between Types

More from this repository10

🎯
tsdoc-comments🎯Skill

Generates TypeScript documentation comments (TSDoc) to explain public APIs, complex types, and provide comprehensive code documentation with IDE tooltips.

🎯
async-over-callbacks🎯Skill

Transforms callback-based asynchronous code into clean, readable async/await patterns for better type flow and error handling.

🎯
type-safe-monkey-patching🎯Skill

Enables type-safe runtime extension of global objects and DOM elements in TypeScript without sacrificing type checking or using `as any`.

🎯
create-objects-all-at-once🎯Skill

Efficiently initializes multiple TypeScript objects simultaneously using concise object literal syntax and spread operators.

🎯
module-by-module-migration🎯Skill

Guides developers through systematic TypeScript module migration, breaking down complex refactoring into manageable, incremental steps.

🎯
ts-js-relationship🎯Skill

Explains TypeScript's relationship to JavaScript, highlighting how it adds static typing and catches errors before runtime while remaining fully compatible with JavaScript code.

🎯
code-gen-independent🎯Skill

Generates JavaScript code despite TypeScript type errors and demonstrates that TypeScript types are erased at runtime, requiring alternative type checking strategies.

🎯
context-type-inference🎯Skill

Helps restore precise type context when extracting values, preventing type inference errors through annotations, const assertions, and type preservation techniques.

🎯
precise-string-types🎯Skill

Enforces strict string type constraints and prevents unintended string type conversions in TypeScript projects.

🎯
type-display-attention🎯Skill

Displays and simplifies complex TypeScript types to improve IDE readability and developer experience.