Sub topic 5
Describing Functions and Callable Objects
This chapter assumes basic TypeScript annotations, JavaScript functions and objects, and the ability to run the TypeScript compiler. The focus is how interfaces and type aliases describe functions, including functions that also have properties.
Function types with type aliases
A function type describes the parameters a function accepts and the value it returns. A type alias is often the clearest form for a function used as a parameter or variable type.
type FormatFn = (input: string) => string;
const trim: FormatFn = (input) => input.trim();
const upper: FormatFn = (input) => input.toUpperCase();
function applyFormat(value: string, format: FormatFn): string {
return format(value);
}
console.log(applyFormat(" hello ", trim)); // helloThe parameter name in a function type is descriptive. The important parts are its type and position. FormatFn accepts one string and returns a string.
A common error is to confuse a function type with the result of calling a function:
const formatter: FormatFn = trim; // correct
const result: string = trim(" text "); // correct
// const wrong: FormatFn = trim(" text "); // error: result is a stringtrim is a function. trim(" text ") is a string.
Callable interfaces
An interface can describe a function with a call signature. A call signature uses parentheses without a method name:
interface Formatter {
(input: string): string;
}
const removeSpaces: Formatter = (input) => input.replaceAll(" ", "");
console.log(removeSpaces("a b c")); // abcFor a function with only parameters and a return value, an interface and a type alias are usually interchangeable:
interface FormatterInterface {
(input: string): string;
}
type FormatterAlias = (input: string) => string;
const a: FormatterInterface = (text) => text.toUpperCase();
const b: FormatterAlias = (text) => text.toLowerCase();TypeScript uses structural typing here. The values do not need to come from a particular declared name. They need a compatible callable shape.
Callable objects with properties
JavaScript functions are objects, so a function can have properties. An interface can describe both the call signature and those properties:
interface Counter {
(step?: number): number;
readonly label: string;
}
function createCounter(label: string): Counter {
let value = 0;
const counter = ((step = 1) => {
value += step;
return value;
}) as Counter;
Object.defineProperty(counter, "label", {
value: label,
writable: false,
enumerable: true,
});
return counter;
}
const counter = createCounter("items");
console.log(counter(), counter(2), counter.label); // 1 3 itemsThe call signature says that counter() is valid. The label property adds information attached to the callable value. readonly prevents assignment through the type, although it does not automatically freeze every runtime object.
How the shapes fit together
Interfaces can also describe overloaded call signatures, where different parameter lists are valid. Keep overloads consistent with the implementation and document the expected behavior.
Exercise
Should you use an interface or a type alias for a value that only accepts a number and returns a boolean?
Show answer
Use a type alias such as type Predicate = (value: number) => boolean. An interface can express it too, but the alias is more direct when no properties are attached.