🎯

currying-inference

🎯Skill

from marius-townhouse/effective-typescript-skills

VibeIndex|
What it does

Enables advanced TypeScript type inference by strategically creating new inference sites using classes and curried functions.

πŸ“¦

Part of

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

currying-inference

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 generic types aren't inferred. Use when builder patterns need better types. Use when creating new inference sites.

Overview

# Use Classes and Currying to Create New Inference Sites

Overview

When TypeScript can't infer generic types, create new inference opportunities.

TypeScript infers generic type parameters at specific "inference sites." When it doesn't have enough information at one site, you can create additional sites using classes, currying, or helper functions.

When to Use This Skill

  • Generic type parameters aren't being inferred
  • Builder pattern needs type inference
  • Chained methods lose type information
  • Creating APIs that guide type inference

The Iron Rule

```

Create inference sites where TypeScript needs type information.

Classes and curried functions provide natural inference points.

```

Remember:

  • Inference happens at function calls and class instantiation
  • More inference sites = better type inference
  • Currying splits inference across multiple calls
  • Classes provide inference at construction

Detection: Missing Inference

```typescript

declare function fetchData(url: string, options: RequestOptions): Promise;

// TypeScript can't infer T

const data = await fetchData('/api/users', { method: 'GET' });

// ^? unknown

// Must specify explicitly

const data = await fetchData('/api/users', { method: 'GET' });

```

The type parameter T has no inference site.

Solution 1: Add Inference Site with Parameter

```typescript

declare function fetchData(

url: string,

parser: (raw: unknown) => T

): Promise;

const data = await fetchData('/api/users', (raw) => raw as User[]);

// ^? User[]

```

The parser function provides an inference site for T.

Solution 2: Curried Functions

```typescript

// Single function: T not inferred

function makeRequest(url: string): Promise;

// Curried: inference at each call

function makeRequest() {

return (url: string): Promise => {

return fetch(url).then(r => r.json());

};

}

// Usage creates inference site

const getUsers = makeRequest();

const users = await getUsers('/api/users');

```

Solution 3: Builder Pattern with Classes

```typescript

class RequestBuilder {

private url: string = '';

setUrl(url: string): this {

this.url = url;

return this;

}

// New method creates new inference site

withParser(parser: (data: unknown) => U): RequestBuilder {

return this as unknown as RequestBuilder;

}

async execute(): Promise {

const response = await fetch(this.url);

return response.json();

}

}

// Type inferred from parser

const users = await new RequestBuilder()

.setUrl('/api/users')

.withParser((data): User[] => data as User[])

.execute();

// ^? User[]

```

Practical Example: Event Emitter

```typescript

// Without inference sites

class EventEmitter {

on(event: string, handler: (data: T) => void): void;

emit(event: string, data: T): void;

}

// TypeScript can't connect the T's

emitter.on('user', (data) => {

// ^? unknown

});

// With type map

interface EventMap {

user: User;

message: Message;

}

class TypedEventEmitter> {

on(

event: K,

handler: (data: Events[K]) => void

): void;

emit(

event: K,

data: Events[K]

): void;

}

const emitter = new TypedEventEmitter();

emitter.on('user', (data) => {

// ^? User

});

```

Factory Functions

```typescript

// Factory provides inference site

function createStore(initial: T) {

let state = initial;

return {

get: () => state,

set: (newState: T) => { state = newState; }

};

}

const userStore = createStore({ name: 'Alice', age: 30 });

// T inferred from initial value

userStore.set({ name: 'Bob', age: 25 }); // OK

userStore.set({ name: 'Charlie' }); // Error: missing age

```

Method Chaining with Type Evolution

```typescript

class QueryBuilder {

select(...keys: K[]): QueryBuilder> {

return this as any;

}

where(predicate: (item: T) => boolean): QueryBuilder {

return this as any;

}

execute(): Selected[] {

// ...

}

}

interface User { id: number; name: string; email: string; age: number; }

const results = new QueryBuilder()

.select('name', 'email')

.where(u => u.age > 18)

.execute();

// ^? { name: string; email: string; }[]

```

Generic Constraints for Better Inference

```typescript

// Without constraint

function pluck(items: T[], key: K): T[K][];

// K not constrained, inference poor

// With constraint

function pluck(items: T[], key: K): T[K][] {

return items.map(item => item[key]);

}

const names = pluck(users, 'name');

// ^? string[]

```

The constraint K extends keyof T helps TypeScript infer K from T.

Avoiding Type Parameters in Return Position Only

```typescript

// Bad: T only in return position (no inference)

function parseJson(): T {

return JSON.parse(data);

}

// Good: T has inference site

function parseJson(parser: (raw: unknown) => T): T {

return parser(JSON.parse(data));

}

// Good: Factory pattern

function createParser() {

return {

parse: (data: string): T => JSON.parse(data)

};

}

const userParser = createParser();

```

Pressure Resistance Protocol

1. "Just Use Type Assertions"

Pressure: "I'll cast with as T"

Response: Assertions bypass type checking. Better to design for inference.

Action: Add inference sites through parameters or currying.

2. "Explicit Type Parameters Work"

Pressure: "Users can just write fn(...)"

Response: Inferred types are less work and less error-prone.

Action: Design APIs where inference works automatically.

Red Flags - STOP and Reconsider

  • Generic function with type parameter only in return type
  • Users always need to specify generic parameters explicitly
  • unknown or any appearing where specific types are expected
  • Type assertions needed to get correct types

Common Rationalizations (All Invalid)

| Excuse | Reality |

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

| "Users can specify the type" | Good API design infers types |

| "It's too complex" | Currying and factories are straightforward |

| "Type assertions work" | They bypass safety; inference is better |

Quick Reference

```typescript

// BAD: No inference site for T

function fetch(url: string): Promise;

// GOOD: Parser provides inference site

function fetch(url: string, parse: (raw: unknown) => T): Promise;

// GOOD: Currying

function fetch() {

return (url: string): Promise => ...;

}

// GOOD: Factory

function createFetcher(parse: (raw: unknown) => T) {

return (url: string): Promise => ...;

}

```

The Bottom Line

Design APIs that give TypeScript inference opportunities.

When generic types can't be inferred, add inference sites: parameters that use the type, curried functions, or class methods. Good API design makes explicit type parameters unnecessary.

Reference

Based on "Effective TypeScript" by Dan Vanderkam, Item 28: Use Classes and Currying to Create New Inference Sites.

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.