JavaScript TypeScript Basics 5 — Questions and Answers
Question 1: What does the non-null assertion operator (`!`) do in TypeScript?
- Negates a boolean value
- Tells the compiler that a value is definitely not null or undefined (Correct answer)
- Throws an error if the value is null
- Converts a value to boolean
Correct answer: Tells the compiler that a value is definitely not null or undefined
The `!` postfix operator asserts to the compiler that the expression is non-null and non-undefined, suppressing null checks.
Question 2: What is the `unknown` type in TypeScript and how does it differ from `any`?
- `unknown` behaves exactly like `any`
- `unknown` requires type narrowing before use, while `any` bypasses type checks entirely (Correct answer)
- `unknown` is only for function parameters, `any` is for all variables
- `unknown` is deprecated in favor of `any`
Correct answer: `unknown` requires type narrowing before use, while `any` bypasses type checks entirely
Unlike `any`, `unknown` forces you to narrow the type before performing operations, making it the type-safe alternative.
Question 3: How do you extend an interface in TypeScript?
- interface B implements A {}
- interface B extends A {} (Correct answer)
- interface B inherits A {}
- interface B includes A {}
Correct answer: interface B extends A {}
TypeScript interfaces use the `extends` keyword to inherit properties from one or more other interfaces.
Question 4: What is a mapped type in TypeScript?
- A type that maps runtime values to strings
- A type that transforms each property of an existing type using a template (Correct answer)
- A function that converts one type to another at runtime
- A type imported from a separate module
Correct answer: A type that transforms each property of an existing type using a template
Mapped types iterate over the keys of a type and transform each property, enabling reusable type transformations.
Question 5: What does `Record<K, V>` represent in TypeScript?
- An ordered list of key-value pairs
- An object type with keys of type K and values of type V (Correct answer)
- A read-only map of keys to values
- A database record type
Correct answer: An object type with keys of type K and values of type V
`Record<K, V>` constructs an object type whose property keys are K and property values are V.
Question 6: Which of the following is an example of a conditional type in TypeScript?
- type A = B extends C ? D : E; (Correct answer)
- type A = B ? C : D;
- type A = B if C else D;
- type A = B ?? C;
Correct answer: type A = B extends C ? D : E;
Conditional types use the `extends` keyword in a ternary-like expression to select between types based on a condition.
Question 7: What happens when you use the `satisfies` operator in TypeScript?
- It casts the value to the given type, losing specific type information
- It validates that a value matches a type while preserving its most specific type (Correct answer)
- It marks a type as deprecated
- It ensures a class implements an interface at runtime
Correct answer: It validates that a value matches a type while preserving its most specific type
The `satisfies` operator (introduced in TypeScript 4.9) checks type compatibility without widening the inferred type of the expression.
What does the non-null assertion operator (`!`) do in TypeScript?