Generic Type Inference

What

In most cases, you don’t need to explicitly provide type arguments when calling a generic function. TypeScript can automatically “infer” the types based on the values you pass in.

How

Simply call the function as you would in normal JavaScript:

function identity<T>(val: T): T { return val; }

// T is automatically inferred as 'string'
const result = identity("hello");

Why

Type inference is a major part of what makes TypeScript feel ergonomic. It allows you to write generic, reusable code that behaves like standard JavaScript while still providing full, background-level type safety without the verbosity of manual type parameters.

Summary

Let TypeScript’s powerful inference engine handle your type parameters whenever possible to keep your code clean.