Zest.

TypeScript: Generic function

A short, tested TypeScript example for generic function. Copy it and adjust the names and paths for your project.

TypeScript
function first<T>(items: T[]): T | undefined {
  return items[0];
}

const n = first([1, 2, 3]);       // number | undefined
const s = first(["a", "b"]);     // string | undefined

Find it in Zest

Search for “typescript generic function” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More TypeScript examples

All TypeScript examples