Typescript Interview Questions

1. What is TypeScript?

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds static types, interfaces, classes, and better tooling like autocompletion and early error detection.

2. What are the main benefits of TypeScript?

  • Type Safety
  • Early Error Detection (compile-time)
  • Better IDE support (autocomplete, refactoring)
  • Object-Oriented Programming
  • Code Readability and Maintainability

3. What are the basic types in TypeScript?

  • string
  • number
  • boolean
  • any
  • void
  • null
  • undefined
  • never
  • unknown

4. What is the difference between any and unknown?

  • any: Turns off type checking. You can do anything.
  • unknown: Safer alternative. You must check the type before usage.
let value: unknown = "hello"; 
if (typeof value === "string") {     
        console.log(value.toUpperCase()); 
} 

5. How to define an interface in TypeScript?

interface User {     
        name: string;     
        age: number; 
}
 Const user: User = { name: "Sahand", age: 30 }; 

6. What is a tuple in TypeScript?

A tuple is a fixed-length, ordered array with specified types.

let person: [string, number] = ["Alice", 25];

7. What are enums in TypeScript?

Enums allow defining a set of named constants.

enum Direction {     
        Up,     
        Down,     
        Left,     
        Right 
} 

8. What is the difference between interface and type?

  • Both can define object shapes.
  • interface is extendable and preferred for object types.
  • type can define unions, intersections, primitives.

9. What is type inference?

TypeScript can automatically detect the type based on assigned value.

let message = "Hello"; // inferred as string

10. What are union and intersection types?

type Union = string | number; 
Type Intersection = { name: string } & { age: number }; 

11. How to make a property optional in an interface?

interface User {     
      name: string;     age?: 
      number; // optional
} 

12. What are generics in TypeScript?

Generics allow creating reusable and flexible components.

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

13. What is the difference between readonly and const?

  • const: Used with variables.
  • readonly: Used with object properties or class members.
interface Person {     
      readonly id: number; 
} 

14. What are type assertions?

Used to tell TypeScript to treat a value as a specific type.

let someValue: any = "Hello";
Let strLength: number = (someValue as string).length; 

15. What is never type?

never represents values that never occur, e.g., functions that throw or loop forever.

function error(): never {     
     throw new Error("Something went wrong"); 
} 

16. What is void type?

void is used for functions that don’t return a value.

function log(): void {     
       console.log("Logging..."); 
} 

17. How does TypeScript support modules?

Use export and import to create and use modules.

// user.ts 
export const name = "Sahand"; 

 // app.ts 
Import { name } from "./user"; 

18. How to use Partial, Required, and Readonly utility types?

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

let partialUser: Partial<User> = { name: "Ali" }; 
let completeUser: Required<User> = { name: "Ali", age: 20 }; 
Let readonlyUser: Readonly<User> = { name: "Ali", age: 20 }; 

19. What is the Record utility type?

const scores: Record<string, number> = {     
        math: 90,     
        English: 85,
 }; 

20. What is this in TypeScript and how to avoid issues with it?

this behaves the same as in JavaScript. Use arrow functions or bind context to avoid losing this.

class Person {     
      name = "Sahand";     
      greet = () => {         
             console.log(`Hi, I’m ${this.name}`);    
      };
}