TypeScript

TypeScript

Top Interview Questions

About TypeScript

What is TypeScript?

TypeScript is an open-source programming language developed and maintained by Microsoft. It is a superset of JavaScript, meaning it builds on JavaScript by adding optional static typing and additional features. Any valid JavaScript code is also valid TypeScript code.

TypeScript was created to help developers write safer, more scalable, and more maintainable code, especially for large applications. It compiles (or “transpiles”) into plain JavaScript, which can run in any browser or JavaScript environment such as Node.js.


Why TypeScript Was Created

JavaScript is a powerful and flexible language, but it is dynamically typed. This means:

  • Errors are often discovered only at runtime

  • Large codebases can become difficult to manage

  • Debugging can be challenging

TypeScript was introduced to solve these problems by adding:

  • Static typing

  • Better tooling and IDE support

  • Early error detection during development


Key Features of TypeScript

1. Static Typing

TypeScript allows developers to define types for variables, function parameters, and return values.

Example:

let name: string = "John";
let age: number = 25;

This helps catch errors at compile time rather than runtime.


2. Type Inference

Even if types are not explicitly declared, TypeScript can infer them automatically.

let count = 10; // inferred as number

3. Interfaces

Interfaces define the structure of an object.

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

They are useful for enforcing consistent object shapes across the application.


4. Classes and Object-Oriented Programming

TypeScript supports object-oriented programming features like:

  • Classes

  • Inheritance

  • Access modifiers (public, private, protected)

Example:

class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

5. Generics

Generics allow developers to write reusable and flexible code.

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

6. Advanced Type System

TypeScript includes powerful type features such as:

  • Union types

  • Intersection types

  • Type aliases

  • Enums

Example:

let value: string | number;

7. Tooling and IDE Support

TypeScript integrates well with modern editors like Visual Studio Code, providing:

  • Autocomplete

  • Type checking

  • Code navigation

  • Refactoring support


How TypeScript Works

TypeScript code goes through a compilation process:

  1. Write TypeScript code (.ts files)

  2. Compile using the TypeScript compiler (tsc)

  3. Output JavaScript code (.js files)

  4. Run the JavaScript code in a browser or runtime environment

TypeScript does not run directly in browsers; it must be compiled into JavaScript first.


Example: TypeScript vs JavaScript

JavaScript Example:

function add(a, b) {
  return a + b;
}

TypeScript Example:

function add(a: number, b: number): number {
  return a + b;
}

In TypeScript, types ensure that only numbers are passed, reducing runtime errors.


Advantages of TypeScript

1. Early Error Detection

TypeScript catches errors during development rather than at runtime, improving code reliability.


2. Better Code Maintainability

Static typing makes large codebases easier to understand and manage.


3. Improved Developer Experience

With strong tooling support, developers benefit from:

  • Autocompletion

  • IntelliSense

  • Inline documentation


4. Scalability

TypeScript is especially useful for large-scale applications and teams working collaboratively.


5. Compatibility with JavaScript

Since TypeScript is a superset of JavaScript, existing JavaScript projects can be gradually migrated.


Disadvantages of TypeScript

1. Learning Curve

Developers must learn additional concepts such as types and interfaces.


2. Compilation Step

TypeScript requires compilation before execution, adding an extra step in development.


3. More Code to Write

Explicit type definitions can sometimes increase code length.


TypeScript vs JavaScript

Feature TypeScript JavaScript
Typing Static Dynamic
Error Detection Compile-time Runtime
Learning Curve Moderate Easy
Tooling Advanced Standard
  • JavaScript is simpler and faster to start with

  • TypeScript is better for large and complex applications


Where TypeScript is Used

TypeScript is widely used in modern development, including:

1. Web Development

Frameworks like Angular are built with TypeScript. It is also commonly used with React and Vue.js.


2. Backend Development

TypeScript is used with Node.js for building scalable server-side applications.


3. Enterprise Applications

Large companies use TypeScript to manage complex codebases and reduce bugs.


4. Open Source Projects

Many modern open-source projects are written in TypeScript due to its maintainability.


Popular Companies Using TypeScript

Several major companies use TypeScript in production:

  • Google

  • Microsoft

  • Slack

  • Airbnb


TypeScript in Modern Development

TypeScript has become a standard in modern web development. Many frameworks and libraries now support or recommend it by default. For example:

  • Angular uses TypeScript as its primary language

  • React projects often use TypeScript for type safety

  • Node.js applications increasingly adopt TypeScript


Basic TypeScript Types

Some commonly used types include:

  • string → text values

  • number → numeric values

  • boolean → true/false

  • array → list of values

  • tuple → fixed-length arrays

  • any → disables type checking

  • void → no return value

Example:

let isActive: boolean = true;
let scores: number[] = [90, 85, 88];

When Should You Use TypeScript?

TypeScript is ideal when:

  • Building large-scale applications

  • Working in teams

  • Maintaining long-term projects

  • Needing better code reliability

It may be less necessary for:

  • Small scripts

  • Quick prototypes

  • Simple applications


Future of TypeScript

TypeScript continues to grow in popularity due to its strong typing system and developer-friendly features. As JavaScript applications become more complex, TypeScript is increasingly seen as a standard for professional development.

With ongoing support from Microsoft and a large community, TypeScript is expected to remain a dominant language in frontend and backend development.


Conclusion

TypeScript is a powerful superset of JavaScript that enhances development by adding static typing, better tooling, and improved maintainability. It helps developers write cleaner, more reliable code and is especially useful for large-scale applications.

By combining the flexibility of JavaScript with the safety of static typing, TypeScript has become a preferred choice for modern developers and organizations worldwide. Whether used with frameworks like Angular or React, TypeScript plays a crucial role in building robust and scalable applications.

Fresher Interview Questions

 

🧠 Basics of TypeScript


1. What is TypeScript?

Answer:
TypeScript is a superset of JavaScript developed by Microsoft that adds static typing.

πŸ‘‰ Key points:

  • Compiles to JavaScript

  • Helps catch errors at compile time

  • Improves code maintainability


2. Why use TypeScript instead of JavaScript?

Answer:

  • Static type checking

  • Better IDE support (autocomplete, IntelliSense)

  • Early error detection

  • Easier to maintain large codebases

  • Supports modern JavaScript features


3. How does TypeScript work?

Answer:

  1. Write TypeScript code (.ts)

  2. Compile using TypeScript compiler (tsc)

  3. Output is plain JavaScript


4. What are types in TypeScript?

Answer:
Types define the kind of data a variable can hold.

Examples:

  • string

  • number

  • boolean

  • array

  • object

  • any

  • unknown


5. What is type inference?

Answer:
TypeScript automatically infers the type of a variable.

let name = "John"; // inferred as string

πŸ”€ Basic Types


6. Difference between any and unknown?

Answer:

any unknown
No type checking Requires type checking
Unsafe Safer

πŸ‘‰ Example:

let value: unknown;

if (typeof value === "string") {
  console.log(value.toUpperCase());
}

7. What is void?

Answer:
Used for functions that return nothing.

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

8. What is never type?

Answer:
Represents values that never occur.

πŸ‘‰ Used in:

  • Infinite loops

  • Functions that throw errors

function throwError(): never {
  throw new Error("Error");
}

🧱 Interfaces and Types


9. What is an interface?

Answer:
Defines the structure of an object.

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

10. Difference between interface and type?

Answer:

interface type
Used for object structure More flexible
Can be extended Can use unions/intersections

11. Can interfaces extend other interfaces?

Answer:
Yes.

interface Person {
  name: string;
}

interface Employee extends Person {
  salary: number;
}

12. What are optional properties?

Answer:
Properties that may or may not exist.

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

βš™οΈ Functions in TypeScript


13. How to define function types?

Answer:
Specify parameter and return types.

function add(a: number, b: number): number {
  return a + b;
}

14. What are optional parameters?

Answer:
Parameters that are not required.

function greet(name?: string) {
  console.log(name);
}

15. What are default parameters?

Answer:
Provide default values.

function greet(name: string = "Guest") {
  console.log(name);
}

16. What are arrow functions in TypeScript?

Answer:
Short syntax with implicit this.

const sum = (a: number, b: number): number => a + b;

🧩 Advanced Types


17. What are union types?

Answer:
A variable can hold multiple types.

let value: string | number;

18. What are intersection types?

Answer:
Combines multiple types into one.

type A = { name: string };
type B = { age: number };

type C = A & B;

19. What are literal types?

Answer:
Restrict values to specific literals.

let direction: "left" | "right";

20. What is type alias?

Answer:
Used to create custom types.

type ID = string | number;

πŸ“¦ Arrays and Objects


21. How to type arrays?

Answer:

let numbers: number[] = [1, 2, 3];

22. How to type objects?

Answer:

let user: { name: string; age: number } = {
  name: "John",
  age: 25
};

πŸ”„ Classes in TypeScript


23. What are classes in TypeScript?

Answer:
Blueprint for creating objects.

class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

24. What are access modifiers?

Answer:

  • public → accessible everywhere

  • private → only inside class

  • protected → accessible in subclass


25. What is inheritance in TypeScript?

Answer:

class Animal {
  move() {}
}

class Dog extends Animal {}

🧠 Generics


26. What are generics?

Answer:
Allow reusable components with type safety.

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

27. Why use generics?

Answer:

  • Reusability

  • Type safety

  • Avoid duplication


⚠️ Type Assertions & Casting


28. What is type assertion?

Answer:
Telling TypeScript about a variable’s type.

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

🌐 Modules in TypeScript


29. What are modules?

Answer:
Used to organize code into separate files.

export const name = "John";
import { name } from "./file";

πŸ§ͺ Practical / Scenario Questions


30. How does TypeScript help in large projects?

Answer:

  • Improves code maintainability

  • Detects bugs early

  • Makes refactoring safer

  • Enhances collaboration


31. What happens if you ignore types?

Answer:
TypeScript will fall back to any, reducing type safety.


32. Difference between compile-time and runtime errors?

Answer:

Compile-time Runtime
Detected before execution Occurs during execution
TypeScript errors JavaScript errors

33. How do you compile TypeScript?

Answer:
Using TypeScript compiler:

tsc file.ts

34. What is tsconfig.json?

Answer:
Configuration file for TypeScript compiler.


🎯 HR + Conceptual Questions


35. Why did you choose TypeScript?

πŸ‘‰ Example:

  • Better type safety

  • Fewer runtime errors

  • Easier debugging


36. What are your strengths?

  • Understanding of types

  • JavaScript fundamentals

  • Problem solving


37. Tell about a TypeScript project

πŸ‘‰ Explain:

  • What you built

  • Types used

  • Challenges

  • Improvements


πŸš€ Final Preparation Tips

βœ” Focus on:

  • Interfaces

  • Generics

  • Functions

  • Union & intersection types

βœ” Practice:

  • Converting JS → TS

  • Writing typed functions

  • Small projects (Todo app in TS)

βœ” Be ready to:

  • Explain type decisions

  • Write small code snippets


 

Experienced Interview Questions

 

🧠 Basics of TypeScript


1. What is TypeScript?

Answer:
TypeScript is a superset of JavaScript developed by Microsoft that adds static typing.

πŸ‘‰ Key points:

  • Compiles to JavaScript

  • Helps catch errors at compile time

  • Improves code maintainability


2. Why use TypeScript instead of JavaScript?

Answer:

  • Static type checking

  • Better IDE support (autocomplete, IntelliSense)

  • Early error detection

  • Easier to maintain large codebases

  • Supports modern JavaScript features


3. How does TypeScript work?

Answer:

  1. Write TypeScript code (.ts)

  2. Compile using TypeScript compiler (tsc)

  3. Output is plain JavaScript


4. What are types in TypeScript?

Answer:
Types define the kind of data a variable can hold.

Examples:

  • string

  • number

  • boolean

  • array

  • object

  • any

  • unknown


5. What is type inference?

Answer:
TypeScript automatically infers the type of a variable.

let name = "John"; // inferred as string

πŸ”€ Basic Types


6. Difference between any and unknown?

Answer:

any unknown
No type checking Requires type checking
Unsafe Safer

πŸ‘‰ Example:

let value: unknown;

if (typeof value === "string") {
  console.log(value.toUpperCase());
}

7. What is void?

Answer:
Used for functions that return nothing.

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

8. What is never type?

Answer:
Represents values that never occur.

πŸ‘‰ Used in:

  • Infinite loops

  • Functions that throw errors

function throwError(): never {
  throw new Error("Error");
}

🧱 Interfaces and Types


9. What is an interface?

Answer:
Defines the structure of an object.

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

10. Difference between interface and type?

Answer:

interface type
Used for object structure More flexible
Can be extended Can use unions/intersections

11. Can interfaces extend other interfaces?

Answer:
Yes.

interface Person {
  name: string;
}

interface Employee extends Person {
  salary: number;
}

12. What are optional properties?

Answer:
Properties that may or may not exist.

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

βš™οΈ Functions in TypeScript


13. How to define function types?

Answer:
Specify parameter and return types.

function add(a: number, b: number): number {
  return a + b;
}

14. What are optional parameters?

Answer:
Parameters that are not required.

function greet(name?: string) {
  console.log(name);
}

15. What are default parameters?

Answer:
Provide default values.

function greet(name: string = "Guest") {
  console.log(name);
}

16. What are arrow functions in TypeScript?

Answer:
Short syntax with implicit this.

const sum = (a: number, b: number): number => a + b;

🧩 Advanced Types


17. What are union types?

Answer:
A variable can hold multiple types.

let value: string | number;

18. What are intersection types?

Answer:
Combines multiple types into one.

type A = { name: string };
type B = { age: number };

type C = A & B;

19. What are literal types?

Answer:
Restrict values to specific literals.

let direction: "left" | "right";

20. What is type alias?

Answer:
Used to create custom types.

type ID = string | number;

πŸ“¦ Arrays and Objects


21. How to type arrays?

Answer:

let numbers: number[] = [1, 2, 3];

22. How to type objects?

Answer:

let user: { name: string; age: number } = {
  name: "John",
  age: 25
};

πŸ”„ Classes in TypeScript


23. What are classes in TypeScript?

Answer:
Blueprint for creating objects.

class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

24. What are access modifiers?

Answer:

  • public → accessible everywhere

  • private → only inside class

  • protected → accessible in subclass


25. What is inheritance in TypeScript?

Answer:

class Animal {
  move() {}
}

class Dog extends Animal {}

🧠 Generics


26. What are generics?

Answer:
Allow reusable components with type safety.

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

27. Why use generics?

Answer:

  • Reusability

  • Type safety

  • Avoid duplication


⚠️ Type Assertions & Casting


28. What is type assertion?

Answer:
Telling TypeScript about a variable’s type.

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

🌐 Modules in TypeScript


29. What are modules?

Answer:
Used to organize code into separate files.

export const name = "John";
import { name } from "./file";

πŸ§ͺ Practical / Scenario Questions


30. How does TypeScript help in large projects?

Answer:

  • Improves code maintainability

  • Detects bugs early

  • Makes refactoring safer

  • Enhances collaboration


31. What happens if you ignore types?

Answer:
TypeScript will fall back to any, reducing type safety.


32. Difference between compile-time and runtime errors?

Answer:

Compile-time Runtime
Detected before execution Occurs during execution
TypeScript errors JavaScript errors

33. How do you compile TypeScript?

Answer:
Using TypeScript compiler:

tsc file.ts

34. What is tsconfig.json?

Answer:
Configuration file for TypeScript compiler.


🎯 HR + Conceptual Questions


35. Why did you choose TypeScript?

πŸ‘‰ Example:

  • Better type safety

  • Fewer runtime errors

  • Easier debugging


36. What are your strengths?

  • Understanding of types

  • JavaScript fundamentals

  • Problem solving


37. Tell about a TypeScript project

πŸ‘‰ Explain:

  • What you built

  • Types used

  • Challenges

  • Improvements


πŸš€ Final Preparation Tips

βœ” Focus on:

  • Interfaces

  • Generics

  • Functions

  • Union & intersection types

βœ” Practice:

  • Converting JS → TS

  • Writing typed functions

  • Small projects (Todo app in TS)

βœ” Be ready to:

  • Explain type decisions

  • Write small code snippets