Sample questions
Question 1Easy
Given let t: [string, number];, which assignment compiles?
t = ["a"];t = ["a", 1, true];t = ["a", 1];t = [1, "a"];
Answer: C. t = ["a", 1];
Explanation
A tuple type fixes both the length and the type at each position: a string first, then a number.
Step-by-step solution
- A tuple type [string, number] fixes the length at two and the type at each position.
- The first element must be a string and the second a number.
- The value ['a', 1] has exactly that shape, so it compiles.
- So the answer is option C.
Why the other options are wrong
- A. A one-element array is too short for the tuple.
- B. A three-element array is too long for the tuple.
- D. The order is wrong: the first element must be a string.
Tip: A tuple checks both length and the type at each index.
Question 2Easy
What does TypeScript report for this code?
type User = { name: string };
type User = { age: number };
- Error: duplicate identifier 'User'
- No error; the second alias wins
- Error only on the first line
- No error; the two type aliases merge into one
Answer: A. Error: duplicate identifier 'User'
Explanation
Type aliases cannot be reopened or merged. Declaring the same alias twice is a duplicate identifier error; only interfaces merge.
Step-by-step solution
- A type alias gives a name to a type, and that name cannot be declared twice.
- Declaring type User twice is a duplicate identifier error.
- Type aliases do not merge, unlike interfaces.
- So the answer is option A.
Why the other options are wrong
- B. There is no silent replacement; TypeScript reports an error.
- C. The error is on the second declaration, and the name is duplicated in the same scope.
- D. Type aliases do not merge; only interfaces do.
Tip: Duplicate type aliases are an error; duplicate interfaces merge.
Question 3Easy
What happens on the last line?
interface Point { readonly x: number; y: number }
const p: Point = { x: 1, y: 2 };
p.x = 5;
- Compile error: p is declared with const, so it is frozen
- Compile error: x is a read-only property
- It compiles and p.x becomes 5
- Runtime TypeError, but it compiles
Answer: B. Compile error: x is a read-only property
Explanation
readonly is a compile-time check that stops reassignment of the property. const on p only stops reassigning p itself, not its properties.
Step-by-step solution
- The property x is declared readonly in the interface.
- Readonly stops assignment to the property at compile time.
- Assigning p.x = 5 is therefore a compile error.
- The answer is option B.
Why the other options are wrong
- A. Const stops reassigning p, not changing its properties, and there is no freezing.
- C. The assignment is blocked, so p.x does not change.
- D. The error is at compile time, and the message is not a runtime TypeError.
Tip: Readonly blocks property writes; const blocks reassigning the variable.
Question 4Easy
What happens when the same interface name is declared twice in one scope, as below?
interface User { name: string }
interface User { age: number }
const u: User = { name: "Asha", age: 21 };
- The second declaration silently replaces the first
- Compile error: duplicate identifier 'User' on line 2
- The declarations merge, so User has both name and age
- Only the first declaration's members are kept
Answer: C. The declarations merge, so User has both name and age
Explanation
Interfaces support declaration merging: repeated declarations of the same interface combine their members. That is how libraries let you extend global types.
Step-by-step solution
- Two interface declarations with the same name in one scope merge.
- The first adds name: string, and the second adds age: number.
- So User has both members, and the object literal with name and age type-checks.
- The answer is option C, the declarations merge.
Why the other options are wrong
- A. Interfaces do not replace each other; they merge.
- B. Duplicate names are an error only for type aliases, not for interfaces.
- D. The members of both declarations are kept, not only the first.
Tip: Interfaces merge when declared twice. Type aliases do not, and a duplicate alias is an error.
Question 5Medium
Does this call compile?
class Dog { name = ""; }
function greet(d: Dog) { return "Hi " + d.name; }
greet({ name: "Tommy" });
- No, only a Dog instance created with new is accepted
- No, classes are compared by their declared names
- Only if Dog is declared as an interface instead
- Yes, because TypeScript compares the shapes of types
Answer: D. Yes, because TypeScript compares the shapes of types
Explanation
TypeScript's type system is structural. Any object with a string name property matches Dog, because Dog has no private members or other properties.
Step-by-step solution
- TypeScript checks types by structure, not by class name.
- Dog has one property, name of type string.
- The object literal { name: 'Tommy' } has that property, so it matches.
- So the call compiles, and the answer is option D.
Why the other options are wrong
- A. Structural typing accepts any object with the right shape, not only instances made with new.
- B. Class names are not compared; shapes are.
- C. Declaring Dog as an interface is not needed for this to compile.
Tip: TypeScript is structural: an object matches a class if it has the same members.
Question 6Medium
What does TypeScript report on the second line?
const xs: readonly number[] = [1, 2];
xs.push(3);
- Error: cannot assign to xs because it is a constant
- No error at compile time, but it throws at runtime
- Error: push does not exist on type 'readonly number[]'
- No error; readonly only stops reassigning xs itself
Answer: C. Error: push does not exist on type 'readonly number[]'
Explanation
readonly number[] (ReadonlyArray<number>) removes mutating methods such as push, pop and splice from the type.
Step-by-step solution
- Readonly number[] is a read-only array type.
- The methods that change the array, such as push, pop and splice, are removed from that type.
- So calling push is a compile error that says push does not exist on the type.
- The answer is option C.
Why the other options are wrong
- A. The error is not about xs being a constant; push is missing from the type.
- B. The error is reported at compile time, not thrown at runtime.
- D. Readonly on the array type blocks push, so there is an error.
Tip: Use readonly T[] for arrays you must not change; push and similar methods are then unavailable.
Question 7Medium
What does TypeScript report for the second line?
let v: unknown = "hello";
v.toUpperCase();
- Error: 'v' is of type 'unknown'
- Error: toUpperCase does not exist on string
- No error; v is inferred as string
- No error; unknown behaves like any
Answer: A. Error: 'v' is of type 'unknown'
Explanation
The declared type unknown is not narrowed by a non-union initialiser, so calling a method needs a check such as typeof v === "string" first.
Step-by-step solution
- unknown requires narrowing.
- You cannot use members directly.
- TypeScript errors that v is unknown.
Why the other options are wrong
- B. The error is about unknown.
- C. It stays unknown.
- D. any would allow it.
Tip: unknown needs a check.
Question 8Medium
What happens when this code is compiled?
interface A { id: string }
interface B { id: number }
interface C extends A, B {}
- Error: C cannot extend both A and B because id conflicts
- C.id becomes string | number
- C.id takes the type from B, the last base listed
- C.id becomes never and it compiles
Answer: A. Error: C cannot extend both A and B because id conflicts
Explanation
When an interface extends several bases, shared properties must have identical (compatible) types. string and number conflict, so the declaration is rejected.
Step-by-step solution
- Interface C extends A and B, so it inherits both id members.
- A says id is string, and B says id is number.
- These types conflict and cannot both hold, so TypeScript rejects the declaration.
- So the answer is option A.
Why the other options are wrong
- B. TypeScript does not combine them into a union; it reports an error.
- C. There is no 'last base wins' rule; the conflict is an error.
- D. The result is an error, not a never type that compiles.
Tip: When extending several interfaces, a shared property must have a compatible type in each.
Question 9Hard
With --strict on, which of these assignments is a compile error?
interface Animal { name: string }
interface Dog extends Animal { bark(): void }
let f1: (a: Animal) => void = (d: Dog) => {}; // line 1
let f2: (d: Dog) => void = (a: Animal) => {}; // line 2
- Only line 1
- Only line 2
- Both lines
- Neither line
Answer: A. Only line 1
Explanation
strictFunctionTypes checks function parameters contravariantly. f1 may be called with any Animal, but the function on the right expects a Dog, which is unsafe. Accepting a wider parameter (line 2) is safe.
Step-by-step solution
- With strictFunctionTypes, function parameters are checked contravariantly.
- Line 1 assigns a function that needs a Dog to a type that will pass any Animal, which is unsafe.
- Line 2 assigns a function that accepts any Animal to a type that passes a Dog, which is safe.
- So only line 1 is an error, the answer is option A.
Why the other options are wrong
- B. Line 2 is safe, because a function taking the wider Animal accepts a Dog.
- C. Line 2 is safe, so it is not an error.
- D. Line 1 is unsafe and is reported as an error.
Tip: Under strict function types, a parameter may be wider than the type it is assigned to, but not narrower.
Question 10Hard
What does TypeScript report on the last line?
interface Options { color?: string; size?: number }
const o = { weight: 10 };
const x: Options = o;
- Error: the value has no properties in common with Options
- No error, because o is not a fresh object literal
- No error, since every property of Options is marked optional
- Error: Object literal may only specify known properties
Answer: A. Error: the value has no properties in common with Options
Explanation
A type whose properties are all optional is a 'weak type'. Assigning something that shares none of its properties is flagged even without excess property checking.
Step-by-step solution
- Options has only optional properties, so it is a weak type.
- The variable o has only weight, which Options does not declare.
- A weak type with no properties in common is rejected, even though o is not a fresh literal.
- So the answer is option A.
Why the other options are wrong
- B. The weak type check applies even to non-fresh values, so the error still happens.
- C. Optional properties do not allow a value with no overlap; the weak type check flags it.
- D. The message is not about excess properties, because o is not an object literal here.
Tip: A weak type (all optional properties) needs at least one matching property.
Practise all 18 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 →