Sub topic 2
Describing Functions and Callable Objects
Assume you know basic TypeScript annotations and JavaScript functions, objects, and classes. This chapter focuses on how types describe callable values and related object shapes.
Describing functions and callable objects
A function type describes its parameters and return value. An interface can describe a callable object with a call signature. A call signature uses parentheses without a method name.
interface Formatter {
(input: string): string;
description: string;
}
const upper: Formatter = Object.assign(
(input: string) => input.toUpperCase(),
{ description: "Converts text to uppercase" },
);
console.log(upper("hello"));
console.log(upper.description);A type alias can describe the function part directly:
type FormatterFn = (input: string) => string;
const trim: FormatterFn = (input) => input.trim();Use a call signature when the value is both callable and has properties. Use a function type when only calling matters.
Index signatures for dictionary-like objects
An index signature describes properties whose names are not known ahead of time. This is useful for dictionaries and lookup tables.
interface Scores {
[player: string]: number;
}
const scores: Scores = {
Ada: 10,
Grace: 12,
};
console.log(scores["Ada"]);The value type applies to every string key. Therefore, a declared property must also be compatible with that type:
interface Settings {
[key: string]: string;
mode: string;
}Avoid using an index signature when the keys are a fixed set. A union of literal keys gives better checking.
Building on existing shapes with `extends`
An interface can extend another interface. The new interface includes the original members and adds more.
interface Named {
name: string;
}
interface Employee extends Named {
employeeId: number;
}
const employee: Employee = { name: "Ada", employeeId: 1 };extends clearly expresses an object hierarchy. The child interface cannot redefine an inherited property with an incompatible type.
Combining types with intersections
An intersection, written with &, creates a type that must satisfy every component.
type Timestamps = {
createdAt: Date;
updatedAt: Date;
};
type EmployeeRecord = Employee & Timestamps;
const record: EmployeeRecord = {
name: "Ada",
employeeId: 1,
createdAt: new Date(),
updatedAt: new Date(),
};Use extends for interface-to-interface relationships. Use intersections when combining aliases or composing independent capabilities.
Unions and literal types: what only aliases can name
A union allows one value to be one of several types. Literal types restrict values to exact strings or numbers. Type aliases can name these combinations directly.
type Status = "idle" | "loading" | "done";
type Result =
| { ok: true; value: string }
| { ok: false; error: Error };
function describe(result: Result): string {
return result.ok ? result.value : result.error.message;
}The ok property lets TypeScript narrow the union safely. Interfaces cannot directly name a union, so use a type alias for alternatives.
Exercise
Should a callable value with a label property use a function type or an interface call signature?
Show answer
Use an interface call signature, because the value must describe both a callable function and its label property.