jpesewang.work
HomeProjectsBlogContact
Back to Blog
TypeScript for Frontend Dev Interviews
Leif Le
September 11, 2025
25 min read

TypeScript for Frontend Dev Interviews

Typescript

Both interface and type can describe the shape of an object, but there are some differences:

1. Interface vs Type

Both interface and type can describe the shape of an object, but there are some differences:

βœ… Similarities:


interface IUser {
id: number;
name: string;
}

type User = {
id: number;
name: string;
};

Both can be used:


const u1: IUser = { id: 1, name: "Alice" };
const u2: User = { id: 2, name: "Bob" };

βœ… Differences:

  1. Extend/Implement:
  2. interface is extendable and works better with classes.
  3. type can use intersection & to combine.

interface Person { name: string; }
interface Developer extends Person { skills: string[]; }

type Animal = { species: string };
type Dog = Animal & { breed: string };
  1. Reopening / Declaration Merging:
  2. Interfaces can be reopened (add properties later).
  3. Types cannot.

interface Window { title: string; }
interface Window { version: string; } // merged automatically
// type cannot do this
  1. Utility Types / Complex Constructs:
  2. type can define unions, tuples, mapped types.
  3. interface cannot.

type Status = "loading" | "success" | "error";
type ApiResponse<T> = { data: T } | { error: string };

πŸ‘‰ Rule of thumb (for interviews):

  1. Use interface when describing object shapes and OOP-style code.
  2. Use type when building unions, advanced types, or aliases.



2. Extending Interfaces

Typical interview question:


interface User {
id: number;
name: string;
}

interface Admin extends User {
role: "admin";
}

const admin: Admin = { id: 1, name: "Quang", role: "admin" };

πŸ‘‰ Shows you understand inheritance in types.

Equivalent with type:


type User = { id: number; name: string };
type Admin = User & { role: "admin" };



3. Generics

Generics make your code reusable and type-safe.

Example 1: Generic Function


function identity<T>(value: T): T {
return value;
}

const str = identity<string>("hello");
const num = identity(123); // TS infers T = number

Example 2: Generic Interface


interface ApiResponse<T> {
data: T;
error?: string;
}

const userResponse: ApiResponse<{ id: number; name: string }> = {
data: { id: 1, name: "Quang" },
};

Example 3: Constraints


function getLength<T extends { length: number }>(item: T): number {
return item.length;
}

getLength("hello"); // βœ… string has length
getLength([1, 2, 3]); // βœ… array has length
// getLength(123) ❌ number has no length

Example 4: React + Generics (interview classic)


function List<T>({ items, render }: { items: T[]; render: (item: T) => JSX.Element }) {
return <ul>{items.map(render)}</ul>;
}

<List
items={["Alice", "Bob"]}
render={(name) => <li key={name}>{name}</li>}
/>;



4. Practice Interview Questions

  1. What’s the difference between type and interface? Which one allows merging?
  2. Extend an interface User into Admin.
  3. Write a generic function getFirst<T>(arr: T[]): T.
  4. Create a generic ApiResponse<T> type for API calls.
  5. Why use generics instead of any?

Related Articles

TypeScript Best Practices for Large Applications

Essential TypeScript patterns and practices that will help you build maintainable applications.

Jan 5, 202410 min read

React Performance Optimization Techniques

Learn advanced techniques to optimize your React applications for better performance.

Dec 28, 202315 min read

Enjoyed this article?

Subscribe to get notified about new articles and updates.