Learn how to annotate function signatures, callback signatures, and define multiple function overload signatures for polymorphic APIs.
Discover how TypeScript infers types automatically and how let versus const declarations shape wide types versus narrow literal types.
Functions in TypeScript require parameter type annotations and optional return type annotations.
You can define separate function type signatures for callbacks:
// Function type signature syntax: (arg1: Type, arg2: Type) => ReturnType
type SearchCallback = (query: string, limit: number) => string[];
const executeSearch: SearchCallback = (q, lim) => {
return [`Result for ${q} (max: ${lim})`];
};
Sometimes a function can accept different combinations of arguments and return different result types depending on those arguments. Function Overloads allow you to define multiple public function signatures followed by a single implementation signature:
// Overload Signature 1: Pass a timestamp number
function makeDate(timestamp: number): Date;
// Overload Signature 2: Pass year, month, day numbers
function makeDate(year: number, month: number, day: number): Date;
// Single Implementation Signature (Internal)
function makeDate(yearOrTimestamp: number, month?: number, day?: number): Date {
if (month !== undefined && day !== undefined) {
return new Date(yearOrTimestamp, month - 1, day);
}
return new Date(yearOrTimestamp);
}
const d1 = makeDate(1600000000000); // Calls Overload 1
const d2 = makeDate(2026, 8, 28); // Calls Overload 2
[ Caller Invocation ] ──> makeDate(2026, 8, 28)
│
▼
[ Signature Matching ] ──> Matches Overload #2 (3 numbers) -> Returns Date
Use overloads for complex API helper functions. Next, let's compare Enums versus Const Assertions!