TypeScript TypeScript Functions & Modules 1 — Questions and Answers
Question 1: What is the correct way to define function overloads in TypeScript?
- Define multiple function bodies with the same name
- Declare multiple call signatures before a single implementation signature (Correct answer)
- Use the `overload` keyword before each variant
- Use union types in both parameter and return positions only
Correct answer: Declare multiple call signatures before a single implementation signature
TypeScript function overloads require declaring multiple call signatures followed by a single implementation signature that handles all overload cases.
Question 2: Which syntax correctly marks a function parameter as optional in TypeScript?
- function greet(name?: string): string (Correct answer)
- function greet(name: string = optional): string
- function greet(optional name: string): string
- function greet(name: string | void): string
Correct answer: function greet(name?: string): string
Optional parameters are marked with `?` after the parameter name, making them assignable to `string | undefined` automatically.
Question 3: What does a `never` return type indicate in TypeScript?
- The function returns undefined when it completes
- The function always returns null
- The function never completes normally — it always throws or loops forever (Correct answer)
- The function has no explicit return statement
Correct answer: The function never completes normally — it always throws or loops forever
`never` represents values that never occur; functions returning `never` either always throw an error or run an infinite loop and never produce a value.
Question 4: How are rest parameters typed in TypeScript?
- function f(...args: any): void
- function f(...args: Array): void
- function f(...args: string[]): void (Correct answer)
- function f(args...: string): void
Correct answer: function f(...args: string[]): void
Rest parameters must be typed as an array type such as `string[]` or `Array<string>` because they collect multiple arguments into a single array.
Question 5: How does `this` behave inside an arrow function in TypeScript?
- It inherits `this` from the enclosing lexical scope at definition time (Correct answer)
- It is always bound to the global object
- It is always typed as `undefined` in strict mode
- It is typed as `any` unless explicitly annotated
Correct answer: It inherits `this` from the enclosing lexical scope at definition time
Arrow functions do not have their own `this` binding; they capture `this` lexically from the surrounding scope where they are defined.
Question 6: What does a `void` return type signify in TypeScript?
- The function must return `null` explicitly
- The return value of the function is not intended to be observed or used (Correct answer)
- The function cannot contain any return statement
- The function returns `undefined` only when called with no arguments
Correct answer: The return value of the function is not intended to be observed or used
`void` indicates the function's return value should not be used; the function may technically return `undefined`, but callers are not expected to rely on that value.
Question 7: Which syntax correctly declares a generic function in TypeScript?
- function identity<T>(arg: T): T { return arg; } (Correct answer)
- function identity[T](arg: T): T { return arg; }
- generic function identity(T)(arg: T): T { return arg; }
- function identity(arg: generic): generic { return arg; }
Correct answer: function identity<T>(arg: T): T { return arg; }
Generic functions declare type parameters using angle brackets `<T>` immediately after the function name, before the parameter list.
What is the correct way to define function overloads in TypeScript?