Skip to content
Justice Tech School

Generics

Generics make code reusable for many types while keeping safety.

Example
function first<T>(a: T[]): T | undefined { return a[0]; }
console.log(first([1, 2]), first(["a"]));

Result

1 a

Exercise

Type parameter.

function id<>(x: T) { return x; }

Quiz Test

What does <T> stand for?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.