TypeScript Reference ยท Keywords and Declarations
TypeScript function declarations
Reference for the function keyword in TypeScript: typed parameters, return types, optional and default parameters, rest, overloads, this parameters and hoisting.
function declares a function. TypeScript lets you annotate each parameter and the return type. Parameter types are required in practice (without them the parameter is an implicit any, an error under strict), while the return type is usually inferred from the body.
A function declaration is hoisted: you can call it above the line that declares it. A function expression (const f = function () {}) is not.
function add(a: number, b: number): number { return a + b; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
function name<T>(param: Type, optional?: Type, withDefault = value, ...rest: Type[]): ReturnType { // body } function name(this: Context, a: number): void {} // this parameter function name(a: string): string; // overload signature function name(a: number): number; // overload signature function name(a: any): any { return a; } // implementation
| Part | Description |
|---|---|
name |
The function name. Omit it for anonymous function expressions. |
<T> |
Optional type parameters. |
param: Type |
A required parameter. Required parameters cannot follow optional ones. |
optional?: Type |
May be omitted; inside the body the type includes undefined. |
withDefault = value |
Default value; the parameter type is inferred from it and the parameter is optional for callers. |
...rest: Type[] |
Collects extra arguments into an array (or tuple). Must be last. |
: ReturnType |
Optional annotation; inferred from the return statements if omitted. |
Errors and edge cases:
- Wrong argument count is error TS2554 ("Expected 1 arguments, but got 2").
- An argument of the wrong type is TS2345 ("Argument of type 'boolean' is not assignable to parameter of type 'string'"); with overloads the message is TS2769 ("No overload matches this call").
- A function with a declared non-void return type that can fall off the end is TS2366 ("Function lacks ending return statement and return type does not include 'undefined'").
- The overload implementation signature is not visible to callers.
Usage notes
- Annotate parameters, infer returns for local helpers. Annotate the return type on exported functions: it documents the contract and catches accidental changes.
thisparameter. A fake first parameter namedthistypes the receiver and is erased from the output. See this.- Overloads are for functions whose return type depends on argument types. Prefer a union parameter or a generic when the logic is the same for all inputs.
- Hoisting. Declarations are hoisted with their body; assigned function expressions are not. TypeScript flags use before assignment of
constfunctions. - Type predicates and assertions. Return types of the form
x is Tandasserts x is Tmake a function a type guard (see type guards). - Generators and async.
function*andasync functionhave their own return type rules; see async and await and yield. - Function types are written
(a: number) => string; the declaration itself is not a type until you taketypeof fn. - Block-level functions in strict mode are scoped to the block.