JavaScript TypeScript Basics 3 — Questions and Answers
Question 1: What does the TypeScript `never` type represent?
- A value that is always undefined
- A type that has no values — used for functions that never return (Correct answer)
- An alias for the `void` type
- A value that is null or undefined
Correct answer: A type that has no values — used for functions that never return
`never` represents the type of values that never occur, such as a function that always throws or has an infinite loop.
Question 2: What is structural typing in TypeScript?
- Types must be explicitly declared for all variables
- Type compatibility is based on the shape (structure) of the type, not its name (Correct answer)
- Types are checked only at runtime
- All types must extend a base class
Correct answer: Type compatibility is based on the shape (structure) of the type, not its name
TypeScript uses structural typing, meaning two types are compatible if they have the same shape, regardless of their names.
Question 3: How do you make a function parameter optional in TypeScript?
- Prefix the parameter with `optional`
- Use a `?` after the parameter name (Correct answer)
- Set the parameter type to `undefined`
- Declare the parameter inside square brackets
Correct answer: Use a `?` after the parameter name
Adding `?` after a parameter name makes it optional, so callers may omit it.
Question 4: What is the difference between `interface` and `type` in TypeScript for defining object shapes?
- Interfaces support generics; type aliases do not
- Type aliases can use union and intersection types; interfaces cannot (Correct answer)
- Interfaces can only have methods, not properties
- There is no difference — they are identical
Correct answer: Type aliases can use union and intersection types; interfaces cannot
While both can define object shapes, `type` aliases support unions, intersections, and mapped types, giving them more expressive power.
Question 5: Which compiler option enforces strict null checks in TypeScript?
- noImplicitAny
- strictNullChecks (Correct answer)
- strictMode
- noNullAllowed
Correct answer: strictNullChecks
The `strictNullChecks` option makes `null` and `undefined` distinct types, preventing accidental null reference errors.
Question 6: What does the `Omit<T, K>` utility type do?
- Picks only the specified keys from type T
- Creates a new type excluding the specified keys K from type T (Correct answer)
- Makes all properties in K required
- Maps all keys of T to boolean
Correct answer: Creates a new type excluding the specified keys K from type T
`Omit<T, K>` constructs a type by removing the properties listed in K from type T.
Question 7: What is a generic constraint in TypeScript?
- A rule that limits the number of generic parameters
- Using `extends` to restrict what types a generic parameter can accept (Correct answer)
- A type that applies to all classes by default
- An interface that all generics must implement
Correct answer: Using `extends` to restrict what types a generic parameter can accept
Generic constraints use `extends` (e.g., `<T extends object>`) to restrict which types may be passed as type arguments.
What does the TypeScript `never` type represent?