Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Function

Generics

Generics parameterize functions, classes, interfaces, and types so one declaration can work with many concrete types while preserving relationships between them. Type arguments may be written explicitly or inferred from usage.

How it works

Generics introduce type parameters that connect related types across a declaration. A function can use the same parameter for an input and its result, while callers may provide the type argument explicitly or let TypeScript infer it; constraints limit which types are accepted.

Syntax

function firstElement<Type>(arr: Type[]): Type | undefined

Parameters

NameTypeDescription
Typetype parameterA type parameter representing the type captured from the argument.
arrType[]An array whose elements have the captured type.

Returns

The first element of the input array, or undefined if the array is empty.

Key points

  • The parameter captures the caller’s type so that later positions in the declaration can refer to that same type.
  • Generic calls commonly omit explicit type arguments because TypeScript can infer them from supplied values.
  • A generic function type places its type-parameter list before the ordinary parameter list.
  • Classes and interfaces can both declare type parameters and use them throughout their members.
  • Providing a default makes a type parameter optional for callers who do not specify it.

Common mistakes

  • A generic implementation cannot assume operations that are unavailable for every possible type argument.
  • Constraints may depend on another type parameter, but the constraint still has to be satisfied by supplied type arguments.
  • A type parameter with a default is optional, so required parameters must appear before defaulted ones.

Example: A generic function preserves an element type

function firstElement<Type>(arr: Type[]): Type | undefined {
  return arr[0];
}

const first = firstElement([10, 20, 30]);
console.log(first);

The compiler infers Type as number, so the returned value is number | undefined.

When to use it

Typed collections

A collection API needs to retain the element type when values are read back.

Reusable utility functions

A helper transforms or forwards a value without losing its specific type.

Public library abstractions

A library exposes containers usable with consumer-defined domain types.

Constrained operations

An API should accept only types possessing a required property.

Interview questions

QWhat problem do generics solve?

A generic type parameter represents a type rather than a runtime value. It lets one declaration preserve relationships between inputs and outputs, such as returning the same element type supplied in an array.

Follow-up: How can the caller avoid writing the type argument? Type argument inference derives it from the value passed to the generic function.

QHow are generic type arguments selected?

The caller can write an explicit type argument, such as identity<number>(42), or omit it and rely on inference. Inference lets the compiler choose the type parameter from the argument.

Follow-up: When inference is insufficient, what is the direct remedy? Specify the type argument explicitly at the call site.

QWhat is a generic constraint?

A constraint restricts a type parameter to types compatible with another type. For example, Type extends Lengthwise permits code to use length while rejecting arguments without that property.

Follow-up: Can a constraint use another type parameter? Yes; the documentation describes constraints whose requirement is expressed in terms of another parameter.

  • Generic functions
  • Generic classes
  • Generic interfaces
  • Type argument inference
  • Generic constraints
  • Default type parameters
  • Array<Type>

Source: TypeScript: Documentation - Generics. Checked 2026-10-15.