yoklainterview sim

Frontend Typescript Type Fundamentals Interview Questions

75 verified Frontend Typescript Type Fundamentals interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Typescript Type FundamentalsDifficulty 1
In TypeScript, what is the main similarity between interface and type alias when describing the shape of an object?
  • aOnly interface can describe an object's shape; type is for primitives only
  • bBoth can describe an object's shape with named properties and types
  • cOnly type can describe an object's shape; interface is for classes only
  • dNeither can describe object shapes; both are limited to function signatures
Explanation:Both interface and type alias can describe an object's shape (named properties with types) — this is their core overlapping use case. They differ in other capabilities (declaration merging, unions, etc.), covered in later questions, but describing object shape is something both do.
Typescript Type FundamentalsDifficulty 2
let id: string | number;
id = "abc";
id = 42;
id = true;

Which line causes a TypeScript compile error?
  • aid = "abc"; — string is not allowed here
  • bid = 42; — number is not allowed here
  • cid = true;
  • dNone of the lines cause an error
Explanation:id's type is the union string | number — only values of type string or number are assignable. "abc" and 42 both fit, but true is a boolean, which is not one of the allowed union members, so it's a type error.
Typescript Type FundamentalsDifficulty 1
type Direction = "up" | "down" | "left" | "right";
let move: Direction = "up";
move = "diagonal";

What happens on the last line?
  • aIt compiles fine, Direction accepts any string
  • bIt compiles fine, but move becomes undefined at runtime
  • cIt throws a TypeError when the code runs
  • dTypeScript compile error — "diagonal" isn't a valid Direction literal
Explanation:Direction is a union of specific string literal types. Only exactly "up", "down", "left", or "right" are assignable to a variable of that type. "diagonal" is a valid string, but not one of those literals, so the compiler rejects the assignment — this is a compile-time error, not a runtime one, since TypeScript's types don't exist after compilation.
Typescript Type FundamentalsDifficulty 2
interface Point {
  readonly x: number;
  readonly y: number;
}
const p: Point = { x: 1, y: 2 };
p.x = 5;

What happens on the last line?
  • aTypeScript compile error — x is readonly
  • bIt compiles and runs fine, p.x becomes 5
  • cIt throws a runtime TypeError because p was declared with const
  • dIt compiles fine but p.x silently stays 1
Explanation:readonly on an interface property means it can only be set once, when the object is created (or in a constructor for classes) — any later assignment is a compile-time error. This is unrelated to const on the variable (which only prevents reassigning p itself, not its properties).
Typescript Type FundamentalsDifficulty 1
interface User {
  name: string;
  nickname?: string;
}

What does the ? after nickname mean?
  • anickname must always be null
  • bnickname can be any type, not just string
  • cnickname is optional, may be omitted
  • dnickname is computed automatically from name
Explanation:The ? marks a property as optional: a value of type User is valid whether or not it includes nickname. If present, it must still be a string; TypeScript effectively treats its type as string | undefined when accessed.
Typescript Type FundamentalsDifficulty 2
interface Named { name: string; }

function greet(x: Named) {
  return `Hi, ${x.name}`;
}

class Dog {
  name: string = "Rex";
  breed: string = "Lab";
}

greet(new Dog());

Does greet(new Dog()) compile, and why?
  • aNo, because Dog doesn't explicitly implements Named
  • bYes, TypeScript uses structural typing — shape match is enough
  • cNo, because Dog has an extra property (breed) not declared in Named
  • dYes, but only because Dog and Named share the same file
Explanation:TypeScript's type system is structural (duck typing), not nominal: a type is compatible if its shape matches, regardless of explicit implements/extends declarations or naming. Dog has a name: string, which is all Named requires — the extra breed property doesn't disqualify it, since having more is fine when passing a variable (as opposed to an object literal, which is checked more strictly for excess properties).

Test yourself against the 2925-question Frontend bank.

Start interview