Sub topic 4
Describing Functions with Function Types
Assume you know JavaScript functions, callbacks, object types, arrays, and union types. You should also be able to run TypeScript with tsc or an editor.
A function type describes the inputs a function accepts and the value it returns. It does not describe the function’s implementation. This lets you store functions in variables, pass them as arguments, and require a particular function shape from other code.
Function type expressions
A function type expression uses this form:
(inputType) => returnTypeFor example, (value: number) => string means “a function that accepts a number and returns a string.”
You can use this type directly or give it a reusable name with a type alias:
type Formatter = (value: number) => string;
function formatPrice(value: number): string {
return `$${value.toFixed(2)}`;
}
const format: Formatter = formatPrice;
console.log(format(12.5)); // $12.50The variable format can refer only to a function with one number parameter and a string return value. The parameter name is not important. The parameter and return types are.
A function type can also describe a callback parameter:
function applyTwice(value: number, transform: (n: number) => number): number {
return transform(transform(value));
}
const result = applyTwice(3, n => n * 2);
console.log(result); // 12The callback receives its type from the transform parameter. The editor can therefore identify n as a number.
| Function type | Meaning |
|---|---|
| `() => void` | Takes no arguments and returns no useful value |
| `(id: number) => string` | Takes a number and returns a string |
| `(left: number, right: number) => number` | Takes two numbers and returns a number |
| `(...items: string[]) => number` | Takes any number of strings and returns a number |
Call signatures for functions with properties
A function type expression describes only a callable function. A call signature describes a callable object that may also have properties:
type Greeter = {
(name: string): string;
language: string;
};
const greeter: Greeter = Object.assign(
(name: string) => `Hello, ${name}`,
{ language: "en" }
);
console.log(greeter("Ada")); // Hello, Ada
console.log(greeter.language); // enThe line (name: string): string is the call signature. It says that greeter("Ada") is valid and produces a string. The language member says that the same value must also have a string property named language.
Choosing a function type
Annotate a function’s declaration when its implementation benefits from an explicit contract. Annotate a variable or property when the place that stores the function should enforce that contract.
Use a type alias when the same function shape appears in several places. Use a call signature when one value must be both callable and able to hold related data.
Common errors
- Writing
string => numberinstead of(value: string) => number. Function parameters need parentheses, even when there is one. - Confusing a function type with a call.
() => numberdescribes a function;() => 42creates one. - Omitting a required return value. A function typed as
(x: number) => stringcannot returnundefined. - Giving a callback a wider or narrower contract without checking whether every allowed call remains safe.
Exercise
Write a type alias named Predicate for a function that accepts a string and returns a boolean. Then use it to type a variable that checks whether a string is empty.
Show answer
type Predicate = (value: string) => boolean; const isEmpty: Predicate = value => value.length === 0;