Sample questions
Question 1Easy
What is IsStr<number>?
type IsStr<T> = T extends string ? "yes" : "no";
- "yes" | "no"
- "yes"
- boolean
- "no"
Answer: D. "no"
Explanation
A conditional type picks the true branch when T is assignable to string. number is not, so the result is "no".
Step-by-step solution
- number does not extend string.
- The false branch is chosen.
- The result is "no".
Why the other options are wrong
- A. That is for boolean.
- B. number is not a string.
- C. Not boolean.
Tip: Check if T extends U.
Question 2Easy
What is C?
const cfg = { port: 3000, host: "localhost" };
type C = typeof cfg;
- "object"
- { port: 3000; host: "localhost" }
- { port: number; host: string }
- object
Answer: C. { port: number; host: string }
Explanation
In a type position, typeof gives the static type of a variable. Properties of a normal object literal widen to number and string.
Step-by-step solution
- Cfg is an object literal with port: 3000 and host: 'localhost'.
- In a type position, typeof cfg gives the static type of cfg.
- Object literal properties widen, so port is number and host is string.
- The answer is option C.
Why the other options are wrong
- A. Typeof gives the object type here, not the string 'object'.
- B. Literal values are widened to number and string for a plain object literal.
- D. Object is too general; the type keeps the property names and types.
Tip: Object literal properties widen to number or string unless the object is declared as const.
Question 3Easy
What is keyof { id: number; name: string }?
- ["id", "name"]
- "id" | "name"
- number | string
- string
Answer: B. "id" | "name"
Explanation
keyof produces a union of an object type's property names as string literal types.
Step-by-step solution
- Keyof gives the property names of the object type.
- Here the names are id and name.
- They are written as a union of string literals.
- The answer is option B.
Why the other options are wrong
- A. Keyof gives a union, not a tuple.
- C. Keyof gives the names as string literals, not the value types.
- D. Keyof gives the literal names, not the general string type.
Tip: Keyof T is a union of the property names of T.
Question 4Easy
In let y: typeof x;, what does typeof do?
- Copies x's compile-time type to y
- Checks the type of x once the program runs
- Returns a string such as "number" at runtime
- Makes y a reference to the variable x
Answer: A. Copies x's compile-time type to y
Explanation
In a type annotation, typeof is a type query evaluated by the compiler. The JavaScript typeof operator is a separate, runtime feature.
Step-by-step solution
- In a type position, typeof x reads the compile-time type of the variable x.
- The statement let y: typeof x; gives y the same type as x.
- This is a compile-time check and emits no runtime code.
- So it copies x's compile-time type to y (option A).
Why the other options are wrong
- B. The type check happens at compile time, not when the program runs.
- C. That describes the runtime typeof operator, not the type query.
- D. Y is a separate variable with the same type, not a reference to x.
Tip: In a type annotation typeof is a type query; in an expression it is the runtime operator.
Question 5Medium
What is Role?
const roles = ["admin", "user"] as const;
type Role = typeof roles[number];
- "admin" | "user"
- string[]
- readonly ["admin", "user"]
- string
Answer: A. "admin" | "user"
Explanation
as const makes the array a readonly tuple of literals. Indexing its type with number gives the union of the element types.
Step-by-step solution
- As const makes roles a readonly tuple of literals: readonly ['admin', 'user'].
- Typeof roles gives that tuple type.
- Typeof roles[number] reads the element types of the tuple.
- The union 'admin' | 'user' is the answer (option A).
Why the other options are wrong
- B. string[] would be the type without as const, and the literals are kept here.
- C. That is the tuple itself, not the union of its elements.
- D. As const keeps the literal types, so the element type is not plain string.
Tip: Use as const together with T[number] to derive a union type from an array of values.
Question 6Medium
What does the -readonly modifier do in this mapped type?
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
- Deletes properties that are readonly
- Makes every property optional
- Adds readonly to each property
- Removes readonly from each property
Answer: D. Removes readonly from each property
Explanation
Mapped type modifiers can be added with + (the default) or removed with -. -readonly strips the readonly modifier.
Step-by-step solution
- The - prefix removes a modifier.
- -readonly removes readonly.
- Properties become mutable.
Why the other options are wrong
- A. No properties are deleted.
- B. That is -? or Partial.
- C. That is +readonly.
Tip: -readonly strips readonly.
Question 7Medium
What is EventName?
type EventName = `on${"Click" | "Hover"}`;
- string
- "onClickHover"
- "onClick" | "onHover"
on${string}
Answer: C. "onClick" | "onHover"
Explanation
A template literal type applied to a union produces one string literal per member.
Step-by-step solution
- A template literal type applied to a union gives one string for each member.
- 'on' followed by Click gives 'onClick'.
- 'on' followed by Hover gives 'onHover'.
- The result is 'onClick' | 'onHover' (option C).
Why the other options are wrong
- A. The type is narrower than string; it is only those two literals.
- B. It is a union of two strings, not one combined string.
- D. That pattern allows any suffix, but here the suffix is only Click or Hover.
Tip: A template literal over a union expands to the union of every combination.
Question 8Medium
What is Unwrap<number>?
type Unwrap<T> = T extends Promise<infer V> ? V : T;
- never
- Promise<number>
- number
- unknown
Answer: C. number
Explanation
number is not a Promise, so the false branch returns T unchanged. Unwrap<Promise<string>> would give string.
Step-by-step solution
- Unwrap checks whether T extends Promise of infer V.
- Number is not a Promise, so the condition is false.
- The false branch returns T unchanged, which is number.
- The answer is number (option C).
Why the other options are wrong
- A. Never only comes from a never input, which is not used here.
- B. Promise<number> is the input type, not the result for number.
- D. Unknown is not returned; T is number.
Tip: A conditional type with infer unwraps only when the pattern matches; otherwise the false branch applies.
Question 9Hard
What does TypeScript report for the last line?
type ToArr<T> = T extends any ? T[] : never;
const x: ToArr<string | number> = [1, "a"];
- Error: the type is string[] | number[], so mixing fails
- No error; the type is any[]
- No error; the type is (string | number)[]
- Error: the type is never
Answer: A. Error: the type is string[] | number[], so mixing fails
Explanation
Conditional types distribute over a union when the checked type is a naked type parameter, giving string[] | number[]. A mixed array fits neither member.
Step-by-step solution
- ToArr is a conditional type on a bare type parameter, so it distributes over the union.
- String | number becomes string[] | number[].
- The array [1, 'a'] has element type string | number, which fits neither string[] nor number[] alone.
- So TypeScript reports an error (option A).
Why the other options are wrong
- B. No error: a mixed array does not fit either member of the union.
- C. The type is string[] | number[], not (string | number)[].
- D. The type is not never; the union is valid, and the assigned value is the problem.
Tip: Conditional types distribute over unions; wrap the checked type in brackets, as [T] extends [any], to stop that.
Question 10Hard
What is keyof { [k: string]: number }?
type K = keyof { [k: string]: number };
- string | number
- number
- never
- string
Answer: A. string | number
Explanation
A string index signature also accepts numeric keys, because JavaScript converts obj[1] to obj["1"]. So keyof includes number.
Step-by-step solution
- The index signature [k: string]: number accepts string keys.
- JavaScript converts numeric keys to strings, so number keys also work on it.
- Keyof therefore includes both string and number.
- The answer is string | number (option A).
Why the other options are wrong
- B. String keys are also included, so number alone is not enough.
- C. A string index signature gives a non-empty key type.
- D. Number keys are also allowed, so number must be in the union.
Tip: Keyof of a string index signature is string | number.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →