TypeScript interview prep
A mix of multiple-choice and code-based questions to check how well you know TypeScript at a senior level.
What is the main difference between type and interface?
Two interfaces with the same name in the same scope are merged. type aliases cannot be reopened this way.
Which utility type extracts the parameter types of a function?
Parameters<T> gives a tuple of the function's parameter types.
const x = { ... } satisfies Type checks that the value matches Type while keeping the more specific inferred type for x.
What is a discriminated union?
A common tag such as kind lets TypeScript narrow the union in switch and if statements.
Which of these is a valid way to define an object that is readonly?
Readonly<T> is the built-in utility that maps every property of T to readonly.
What is the purpose of as const?
as const infers the most literal types possible, which is useful for discriminated union tags and tuple types.
What does infer do inside a conditional type?
type ReturnOf<T> = T extends (...args: any[]) => infer R ? R : never extracts R from the function return type.
Which keyword turns a function parameter type guard into a user-defined type guard?
function isString(x: unknown): x is string { ... } lets callers narrow unknown to string.
What is the structural type system?
TypeScript uses structural typing: an object is assignable if it has all required members with compatible types, regardless of the declared name.
Which of the following cannot be used to narrow unknown?
x as number is a type assertion, not a runtime check. It does not narrow safely at runtime.
type Nullable<T> = { [K in keyof T]: T[K] | null } is a mapped type.
keyof T gives the union of all known public property keys of T.
Which built-in utility removes a set of keys from a type?
Omit<T, K> removes the keys in K from T.
What is the difference between Exclude and Omit?
Exclude<T, U> removes union members. Omit<T, K> removes keys from an object type.
When would you use NoInfer<T>?
NoInfer<T> is useful when you want a generic to keep a wider type and not narrow to a literal from the passed argument.
What is an assertion signature?
function assertIsString(x: unknown): asserts x is string { ... } tells the compiler the argument is narrowed if the function returns.
What does this in a function type parameter do?
function fn(this: Window) { ... } declares the type of this for that function without adding a runtime parameter.
What is the result of NonNullable<string | null | undefined>?
NonNullable<T> removes null and undefined from T.
Which of the following best describes a branded type?
type UserId = string & { readonly __brand: 'UserId' } creates a nominal-like distinction without runtime cost.
Code quiz
What is the inferred type of value?
const config = {
host: 'localhost',
port: 3000,
} as const
const value = config.port
The type of value is 3000, not number, because as const makes every member a literal type.
Does this code compile? If it does, what does it print?
function logLength(x: string | number) {
if (typeof x === 'string') {
console.log(x.length)
} else {
console.log(x.toFixed(2))
}
}
logLength(42)
The typeof check narrows x to string in the if branch and to number in the else branch, so toFixed(2) is valid.
What is wrong with this generic constraint?
function getLength<T extends { length: number }>(x: T) {
return x.length
}
console.log(getLength(42))
number does not satisfy T extends { length: number }, so the call is rejected.
What is the type of result?
type Action =
| { type: 'increment'; value: number }
| { type: 'decrement'; value: number }
| { type: 'reset' }
function isReset(action: Action): action is { type: 'reset' } {
return action.type === 'reset'
}
const action: Action = { type: 'reset' }
const result = isReset(action)
result is boolean. The type guard narrows action, not the returned result itself.
What does this conditional type resolve to?
type Item<T> = T extends (infer E)[] ? E : never
type X = Item<string[]>
X is string. The conditional type extracts the element type E from an array.