TypeScript Reference ยท Keywords and Declarations
TypeScript arrow functions
Reference for arrow functions in TypeScript: parameter and return annotations, contextual typing, generic arrows, object literal returns, lexical this and common pitfalls.
An arrow function is a compact function expression written (params) => body. It has no this, arguments or super of its own; it uses those of the surrounding scope. It cannot be used with new and cannot be a generator.
TypeScript's big gift to arrow functions is contextual typing: when an arrow is passed where a function type is expected, its parameters get their types from that context, so you rarely annotate them.
const square = (n: number): number => n * n; const names = ["a", "b"].map((s) => s.toUpperCase()); // s is inferred as string
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
(param: Type, other = 1): ReturnType => expression async (param: Type): Promise<R> => { ... } <T,>(value: T): T => value (param) => ({ key: value })
| Part | Description |
|---|---|
param: Type |
Annotation. Optional when contextual typing supplies the type. |
: ReturnType |
Optional return annotation, placed after the parameter list. |
<T,> |
Type parameters. In .tsx files write <T,> or <T extends unknown> so it is not parsed as JSX. |
| Expression body | A single expression is returned implicitly. |
| Block body | Needs return. |
Errors and edge cases:
- An unannotated parameter with no context is error TS7006 ("Parameter 'x' implicitly has an 'any' type") under
noImplicitAny. - A single unparenthesized parameter is allowed in JavaScript (
x => x), but a type annotation requires parentheses. - Arrow functions cannot be called with
new: the compiler rejects it because the arrow type has no construct signature, and plain JavaScript throws aTypeError. - A
thisparameter on an arrow is error TS2730 ("An arrow function cannot have a 'this' parameter").
Usage notes
- Return type inference. The return type comes from the body. Add an annotation to catch mistakes at the arrow rather than at its callers.
- Lexical
this. Useful for callbacks and class fields. As a class field an arrow creates one function per instance, which costs memory compared to a prototype method. - Contextual typing works through unions and generics, but it can fail when the callee is overloaded or generic in a way that depends on the arrow.
- Object literal return.
() => { a: 1 }is a block with a label, not an object. Use() => ({ a: 1 }). - Recursion. An arrow assigned to a
constcan call itself by name, but TypeScript may need an explicit annotation to infer its return type: error TS7023 says the function "implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions". - Methods vs properties. Arrow-typed properties are checked contravariantly in parameters; method syntax is bivariant.
Related
- function, return, this, async and await
- Lessons: Functions, Function types, this in functions