← Back to libraryQuestion 217 of 468
🔷TypeScriptBeginner

Functions and Typing

📌 Definition:

TypeScript types function parameters and return values, supports optional/default/rest parameters, and can type the function itself (function types) for callbacks.

📖 Detailed Explanation:

function add(a: number, b: number): number { return a + b } types inputs and output; the return type is often inferred but explicit annotations lock the contract. Optional params use ? (and must follow required ones), defaults use =, and rest params use ...args: number[]. You type a callback with a function type: (value: string, index: number) => void, or type Handler = (e: Event) => void. Async functions return Promise<T>. Function overloads declare multiple signatures for one implementation. In test frameworks, precise function types make custom commands/helpers self-documenting and catch wrong-argument bugs.

🔑 Key Points:
  • Type params and return: (a: number): number
  • Optional (?), default (=), rest (...args: T[]) parameters
  • Callback/function types: (x: string) => void
  • async returns Promise<T>; overloads for multiple signatures
🌍 Real-World Example:

A typed retry helper: function retry<T>(fn: () => Promise<T>, times: number): Promise<T> makes callers pass an async function and get back a correctly-typed promise, with wrong usages flagged.

🎯 Scenario-Based Interview Question:

function log(msg: string, level?: string, tag: string) — TypeScript errors. Why?