Return Type Annotations
TS · Type Annotationssyntax
function fn(): ReturnType { ... }
const fn = (): ReturnType => { ... };example
function findUser(id: string): User | undefined {
return userDatabase.get(id);
}
// Arrow function return type
const formatCurrency = (amount: number): string => {
return `$${amount.toFixed(2)}`;
};
// Async function
async function fetchOrder(id: string): Promise<Order> {
const resp = await fetch(`/api/orders/${id}`);
return resp.json();
}output
// Async functions always return Promise<T>Note Explicit return types are especially valuable for public API functions, async functions, and functions with multiple return paths. They also speed up type-checking in large projects since the compiler does not need to analyze the function body.