Syntax
function name(param: type): returnType { }Examples
Parameters and Return Types
The fundamentals of typing a function signature.
function greet(name: string, greeting: string = "Hello"): string {
return `${greeting}, ${name}!`;
}
function logResult(value: number, label?: string): void {
// label is optional - type is "string | undefined"
console.log(label ? `${label}: ${value}` : value);
}
greet("Alice"); // uses default greeting
greet("Bob", "Hi"); // overrides itTyping a Function as a Value
Describing the shape of a function stored in a variable or passed as an argument.
let mathOperation: (a: number, b: number) => number;
mathOperation = (a, b) => a + b; // parameter types are inferred from the variable's type
function applyOperation(a: number, b: number, operation: (x: number, y: number) => number): number {
return operation(a, b);
}
console.log(applyOperation(5, 3, (x, y) => x * y)); // 15Best practices
- Always type function parameters explicitly - TypeScript cannot infer them the way it infers return types
- Place optional parameters after all required parameters in a function signature - required parameters cannot follow optional ones
- Let TypeScript infer the return type in most cases rather than annotating it explicitly, unless the function is part of a public API where an explicit contract is valuable
- Use a function type expression when a variable or parameter itself needs to hold a function, describing its expected signature precisely
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.