Common Questions and Practical Coding Scenarios - Textnotes

Common Questions and Practical Coding Scenarios


Prepare for TypeScript interviews with common questions, detailed explanations, and practical coding scenarios. This module covers essential topics for developers to confidently tackle technical interviews and assessments.

1. Common TypeScript Interview Questions

Question 1: What is TypeScript and how is it different from JavaScript?

Answer:

TypeScript is a superset of JavaScript that adds static typing, interfaces, enums, and advanced type features. Unlike JavaScript, TypeScript performs compile-time type checking, reducing runtime errors. It also supports modern ECMAScript features and integrates with IDEs for better code completion.

Question 2: What are the benefits of using TypeScript in large projects?

Answer:

  1. Early detection of errors during compile time
  2. Improved readability and maintainability through types and interfaces
  3. Better tooling support (autocomplete, refactoring)
  4. Strong typing helps teams avoid runtime bugs
  5. Easier code refactoring for large codebases

Question 3: Explain the difference between any, unknown, void, and never.

Answer:

  1. any: Disables type checking, allows any value.
  2. unknown: Safer alternative to any, requires type assertion before usage.
  3. void: Indicates a function does not return a value.
  4. never: Represents values that never occur, e.g., a function that throws an error.

Question 4: How does TypeScript handle type inference?

Answer:

TypeScript automatically infers types from variable initialization and function returns. Explicit types can override inference, improving readability and ensuring stricter type safety.


let count = 10; // inferred as number
let name: string = "Muni"; // explicit typing

Question 5: Explain the difference between interface and type in TypeScript.

Answer:

  1. Interface: Can extend other interfaces, mainly used to define object shapes.
  2. Type alias: Can represent objects, unions, intersections, and primitive types.
  3. Key difference: Interfaces can be merged; types cannot.

Question 6: What are generics, and why are they useful?

Answer:

Generics allow defining reusable components or functions while preserving type safety. They prevent using any and allow type parameters to be specified at usage.


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

const result = identity<number>(42);

Question 7: How does TypeScript support advanced type features like Union, Intersection, and Literal types?

Answer:

  1. Union types: Accept multiple types (string | number).
  2. Intersection types: Combine multiple types into one (A & B).
  3. Literal types: Restrict variables to specific values ("admin" | "user").

These features enable flexible yet type-safe code.

2. Practical Coding Scenarios

Scenario 1: Validate API response using DTOs

Problem: Create a type-safe API response validation for a user object with id, name, and optional email.


interface UserDTO {
id: number;
name: string;
email?: string;
}

function validateUser(user: any): user is UserDTO {
return typeof user.id === "number" && typeof user.name === "string";
}

const apiResponse = { id: 1, name: "Muni" };

if (validateUser(apiResponse)) {
console.log(apiResponse.name); // Type-safe access
}

Explanation: Use TypeScript's type guards to ensure correct types and safe property access.

Scenario 2: Implement a generic function for array filtering


function filterArray<T>(arr: T[], predicate: (item: T) => boolean): T[] {
return arr.filter(predicate);
}

const numbers = [1, 2, 3, 4];
const evenNumbers = filterArray<number>(numbers, n => n % 2 === 0);

Explanation: Generics allow creating reusable functions while maintaining type safety.

Scenario 3: Implement Role-Based Access Control


type Role = "admin" | "user";

function checkAccess(role: Role) {
if (role === "admin") return "Full Access";
return "Limited Access";
}

console.log(checkAccess("user")); // Limited Access

Explanation: Using literal types ensures only allowed roles are passed, preventing runtime errors.

Scenario 4: Type-safe Redux state


interface State {
counter: number;
}

const initialState: State = { counter: 0 };

function reducer(state: State = initialState, action: { type: string }) {
switch (action.type) {
case "INCREMENT":
return { counter: state.counter + 1 };
default:
return state;
}
}

Explanation: Interfaces define state shapes, preventing invalid modifications and improving maintainability.

Conclusion

Preparing for TypeScript interviews requires understanding both theoretical concepts and practical scenarios. Mastering common questions, type system features, and coding exercises ensures confidence in technical interviews and real-world problem solving.