Answer: A reusable component that can work with a variety of types.
Generics allow you to create reusable components (functions, classes, interfaces) that can work over a variety of types rather than a single one.
2. What is a decorator in TypeScript?
Advanced
@sealed class Greeter { }
Answer: A special kind of declaration that can be attached to classes, methods, or properties.
Decorators are an experimental feature that provide a way to add annotations and meta-programming syntax for class declarations and members.
3. What is the purpose of `declare` keyword?
Advanced
declare var MyLibrary: any;
Answer: To tell the compiler that a variable has been created elsewhere.
The `declare` keyword is used to tell the TypeScript compiler that a variable, function, or class exists, even if it cannot see its definition (e.g., it comes from a script tag in HTML).
4. What is the `keyof` type operator used for?
Intermediate
type Point = { x: number; y: number }; type P = keyof Point;
Answer: To get a union of the keys of a type.
The `keyof` operator takes an object type and produces a string or numeric literal union of its keys. For `Point`, `P` would be '"x" | "y"'.
5. What does the `typeof` type operator do?
Intermediate
let s = "hello"; let n: typeof s;
Answer: Infers the type of a value.
In a type context, `typeof` infers the type of a variable or property, allowing you to create new variables with the same type.
6. What is the `infer` keyword used for?
Advanced
type Unpack<T> = T extends (infer U)[] ? U : T;
Answer: To declare a type variable to be inferred within a conditional type.
`infer` is a powerful keyword used inside the `extends` clause of a conditional type to capture and use a part of the type being checked.
7. What is a distributive conditional type?
Advanced
type ToArray<T> = T extends any ? T[] : never;
Answer: A conditional type that distributes over naked type parameters in a union.
When the checked type in a conditional is a "naked" generic type parameter (like `T`), the conditional type becomes distributive. If you pass a union to it, it will apply the logic to each member of the union.
8. What is a type guard?
Intermediate
function isString(x: any): x is string { return typeof x === "string"; }
Answer: A special function that also narrows a type within its scope.
A type guard is a function whose return type is a type predicate (`x is string`), which narrows the type of a variable within a conditional block.
9. How can you prevent a conditional type from being distributive?
Advanced
type ToArray<T> = [T] extends [any] ? T[] : never;
Answer: By wrapping the checked type and the constraint in square brackets.
Wrapping each side of the `extends` check in square brackets (e.g., `[T] extends [U]`) prevents the distributive behavior.
10. How can you use the `in` operator as a type guard?
Intermediate
if ("property" in object) { ... }
Answer: To check if a property exists on an object, narrowing its type.
The `in` operator checks for the presence of a property and can be used in a conditional to narrow the type of an object.
11. What is the purpose of an abstract class?
Intermediate
abstract class Animal { abstract makeSound(): void; }
Answer: It serves as a base class for other classes to extend.
Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly and may contain abstract methods that must be implemented by derived classes.
12. What is the `ConstructorParameters<T>` utility type?
Advanced
type T0 = ConstructorParameters<ErrorConstructor>;
Answer: Constructs a tuple or array type from the types of a constructor's parameters.
`ConstructorParameters<T>` extracts the parameter types from a constructor function type. `T0` would be `[string | undefined]`.
13. What is method overloading in TypeScript?
Intermediate
function add(a: number, b: number): number; function add(a: string, b: string): string;
Answer: Having multiple method signatures for a single function body.
TypeScript allows you to provide multiple function signatures (overloads) for a single function, with one final implementation that handles all cases.
Answer: The compiler merging multiple interface declarations with the same name.
If you declare the same interface multiple times, the compiler merges their properties into a single interface. This does not work with `type` aliases.
15. How do you import a type or interface only?
Intermediate
import type { User } from "./user";
Answer: import type { User } from './user';
Using `import type` ensures that the import is only used for type annotations and is guaranteed to be erased at compile time.
16. What is a "variadic tuple type"?
Advanced
type StrNum = [string, ...number[]];
Answer: All of the above.
Variadic tuple types allow for modeling function arguments with `...` in tuples, providing better type safety for functions that take a variable number of arguments.
17. What does the `Awaited<T>` utility type do?
Advanced
type P = Awaited<Promise<Promise<string>>>;
Answer: It recursively unwraps the `await`ed type of a Promise.
`Awaited<T>` is used to model the result of an `await` expression, recursively unwrapping nested promises to get the final resolved value. `P` would be `string`.
18. What is a conditional type?
Intermediate
type IsString<T> = T extends string ? "yes" : "no";
Answer: Both B and C.
Conditional types take the form `T extends U ? X : Y` and allow for powerful type-level logic, similar to a ternary operator in JavaScript.
19. What does the `infer` keyword do?
Intermediate
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
Answer: Declares a new generic type variable within a conditional type.
The `infer` keyword is used within the `extends` clause of a conditional type to declare a new generic type variable that will be inferred by the compiler.
20. What is the purpose of the `this` parameter in a function?
Advanced
function fn(this: SomeType, x: number) { ... }
Answer: It specifies the type of `this` inside the function.
TypeScript allows you to declare the type for `this` in a function by providing a `this` parameter. This parameter is erased during compilation.
21. What is "definite assignment analysis"?
Advanced
let x!: number;
Answer: A feature that analyzes code to see if variables are always assigned.
With `strictPropertyInitialization` on, TypeScript checks that class properties are initialized. The `!` post-fix on a property (`x!`) is a "definite assignment assertion" to tell the compiler it will be initialized elsewhere.
22. What is a "const assertion"?
Intermediate
let x = "hello" as const;
Answer: Tells the compiler to infer the most specific type possible.
A `const` assertion tells TypeScript that the value is fixed. For literals, it infers a literal type; for objects, it makes properties `readonly`.
23. What is a "symbol" in TypeScript?
Advanced
const sym = Symbol();
Answer: A primitive type for creating unique, anonymous object keys.
Symbols are a primitive data type, same as number or string, that are guaranteed to be unique. They are often used as keys for object properties to avoid name clashes.
24. How do you define a function that takes another function as an argument (a callback)?
Beginner
function doSomething(callback: () => void) { ... }
Answer: (callback: () => void)
The syntax `() => void` describes a function that takes no arguments and returns nothing, which is a common way to type simple callbacks.
Ready to test yourself?
The full TypeScript bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.