Syntax

TypeScript Generic Function Syntax

In TypeScript, generic function: writes a function that works with any type — function first<T>(arr: T[]): T { return arr[0]; }

Quick Answer

Syntax
function first<T>(arr: T[]): T { return arr[0]; }
TypeScriptSyntax

Generic Function

Code example
function first<T>(arr: T[]): T { return arr[0]; }

Writes a function that works with any type.

Learn More

How to generic function in TypeScript

  1. Open your TypeScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

TypeScript Generic Function Syntax

Use function first<T>(arr: T[]): T { return arr[0]; }.

Related Syntax References

Frequently asked questions

What is the TypeScript syntax for generic function?

Use this pattern: function first<T>(arr: T[]): T { return arr[0]; }

Can I copy this TypeScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this TypeScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify