TypeScript type inference patterns and techniques
View on GitHubplugins/aai-stack-typescript/skills/typescript-inference/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-stack-typescript/skills/typescript-inference/SKILL.md -a claude-code --skill typescript-inferenceInstallation paths:
.claude/skills/typescript-inference/# TypeScript Inference Skill
Patterns for leveraging TypeScript's type inference capabilities.
## Basic Inference
### Variable Inference
```typescript
// TypeScript infers types from initialization
const name = 'John' // type: string
const age = 30 // type: number
const isActive = true // type: boolean
const items = [1, 2, 3] // type: number[]
// Literal types with const
let status = 'active' // type: string
const mode = 'dark' as const // type: 'dark'
// Object inference
const user = {
name: 'John',
age: 30,
} // type: { name: string; age: number }
// const assertion for readonly literal object
const config = {
api: '/api/v1',
timeout: 5000,
} as const
// type: { readonly api: "/api/v1"; readonly timeout: 5000 }
```
### Function Return Inference
```typescript
// Return type inferred from return statements
function add(a: number, b: number) {
return a + b // return type: number
}
function getUser(id: string) {
return { id, name: 'John', active: true }
} // return type: { id: string; name: string; active: boolean }
// Async function inference
async function fetchData() {
const response = await fetch('/api/data')
return response.json() as Promise<Data>
} // return type: Promise<Data>
```
### Generic Inference
```typescript
// Type parameter inferred from argument
function identity<T>(value: T): T {
return value
}
const str = identity('hello') // T inferred as string
const num = identity(42) // T inferred as number
// Multiple type parameters
function pair<T, U>(first: T, second: U) {
return [first, second] as const
}
const result = pair('name', 42) // [T, U] inferred as [string, number]
```
## Contextual Typing
### Callback Parameters
```typescript
// Parameter types inferred from context
const numbers = [1, 2, 3, 4, 5]
// 'num' inferred as number from array type
const doubled = numbers.map(num => num * 2)
// Event handler inference
document.addEventListener('click', event =