Top 50 TypeScript Interview Questions With Answers for 2025
TypeScript has positioned itself as one of the most important languages a developer could work with for big applications. The increased demand for TypeScript comes with increased demands from candidates in terms of interview preparation. This is a compilation of top 50 TypeScript interview questions and answers that you might encounter in 2025.
1. What is TypeScript?
TypeScript is an object-oriented language that simply adds static types to JavaScript. It’s designed by Microsoft for developers to find mistakes at compile-time instead of runtime while maintaining a clean code base.
2. What is in it for TypeScript?
- Static Typing: Catches the bug early.
- Improving IDE Support: Greatly improve autocompletion and navigation.
- Modern JavaScript Features: It supports ES6+ features.
- Better Refactoring: The code refactoring is easier since the type information exists. Better Documentation: Types document something.
3. How would you declare a variable in TypeScript?
Answer: You can declare a variable by using one of let, const, or var followed by typing annotation. For example:
let age: number = 30;
const name: string = "Alice";
4. What is an interface in TypeScript?
Answer: Typescript Interfaces specify what shape the object should take. It could specify properties and methods, thus it might allow better structuring and type checking.
interface Person {
name: string;
age: number;
}
5. How to declare a class in TypeScript?
class Animal {
constructor(public name: string) {}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
6. What is the difference between an interface and a type?
Interfaces
should be extended or implemented, not types.- where
type
can represent both, primitive types, union and intersection, interface is bound to object shapes.
7. What are generics in TypeScript?
Answer: Generics allow the making of reusable components in such a way that they can, therefore, work with any data type. It enables one to create functions, classes, or interfaces that can operate on many types without sacrificing type safety.
function identity<T>(arg: T): T {
return arg;
}
8. How do you handle null and undefined in TypeScript?
Answer: This is because TypeScript has direct strict null checks. If the compiler option –strictNullChecks is on, then unless it says so, things can’t be null or undefined.
let name: string | null = null;
9. What is an enum in TypeScript?
Answer: An enumeration is one of the ways to declare a set of named constants, numeric, or string-based.
enum Color {
Red,
Green,
Blue,
}
10. What is Tuple in TypeScript?
Answer: A tuple is an immutable array whose elements may be of any type.
let tuple: [string, number] = ["Alice", 30];
11. How to create a union type?
Answer: The union type defines that a variable can hold more than one type, which is defined using the pipe symbol |
.
let id: number | string;
12. What is the ‘any’ type in TypeScript?
Answer: The any
type is there for situations when type-level escape hatch from the type system is needed. It can hold any value, but should never be used to defeat type-safety.
let data: any = 42;
data = "Hello";
13. How would you declare a type of function?
Answer: You define a function type using the following syntax:
let add: (x: number, y: number) => number;
add = (x, y) => x + y;
14. What are type assertions?
Answer: Type assertions allow bypassing of TypeScript’s type inference – examples being the as keyword, angle bracket syntax.
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
15. What is the difference between public, private, and protected?
public
: Access anywhere.private
: Visible only within the class itself.protected
: Within the class and in the derived classes.
16. What does the readonly modifier serve for?
Answer: The readonly modifier provides immutability to a property once initialized. Classes and interfaces make use of it so they cannot be modified.
class Point {
readonly x: number;
readonly y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
17. How is inheritance done in TypeScript?
Answer: It is implemented through the keyword extends in TypeScript. That means a derived class is able to inherit properties and methods from the base class by using it.
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
18. What are decorators in TypeScript?
Answer: Decorators are special annotations that may attach classes, methods, or properties to extend their behaviors. They’re a way of metaprogramming.
function log(target: any, propertyName: string, descriptor: PropertyDescriptor) {
console.log(`Method ${propertyName} was called`);
}
19. How would you handle asynchronous programming in TypeScript?
Answer: Typescript handles asynchronous programming using async and await keywords. This enables declaration of asynchronous code, which is top-to-bottom-written much like synchronous code.
async function fetchData() {
const response = await fetch("https://api.example.com/data");
const data = await response.json();
return data;
}
20. What does the never type do?
Answer: The Never type denotes values that will never happen. Use it for functions that always throw an exception or lead to an infinite loop.
function error(message: string): never {
throw new Error(message);
}
21. How do you declare a namespace in TypeScript?
Answer: Namespaces are used for organizing the code and also for avoiding naming conflicts. You declare a namespace by using the keyword namespace.
namespace MyNamespace {
export class MyClass {
constructor() {
console.log("MyClass instance created");
}
}
}
22. What is the difference between == and === in TypeScript?
Answer: == operator does the equality check with type coercion and === is strict equality check without type coercion. Always usage of === is advisable to avoid any unexpected results.
23. How would you use modules in TypeScript?
Answer: In TypeScript, modules are files that export and import the code. You probably could export variables, functions, or classes, using some sort of export keyword, and then import them in another module using some sort of import keyword.
// In file math.ts
export function add(x: number, y: number): number {
return x + y;
}
// In another file
import { add } from './math';
24. What is ‘unknown’ in TypeScript?
Answer: It’s the safer alternative for the ‘any’ type. The ‘unknown’ type represents any value, but before you perform some operation on it, you do some kind of type checking.
let value: unknown;
value = 5;
if (typeof value === "number") {
console.log(value + 10); // Safe to use
}
25. How would you create a mapped type?
Answer: Mapped types are those kinds of types that take a property and transform/derive another type from it using the keyof operator to iterate on keys.
type Readonly<T> = {
readonly [K in keyof T]: T[K];
};
26. What does the ‘as’ keyword in TypeScript take the role of?
Answers: The ‘as’ keyword is used to tell TypeScript that you want to assert the type of something to be of a particular type.
let someValue: any = "Hello, TypeScript!";
let strLength: number = (someValue as string).length;
27. How would you declare a default parameter in a function?
Answer: You declare a default parameter by assigning a default value to said parameter in the signature of the function.
function greet(name: string = "Guest"): string {
return `Hello, ${name}!`;
}
28. What does the this keyword do in TypeScript?
Answer: this keyword refers to an object representing the current instance of a class or object; it takes on different values depending on the context it is used, especially when dealing with callbacks.
29. How would you declare a conditional type in TypeScript?
Answer: With the release of conditional types, one can create types that depend on conditions in those types. It will be denoted using syntax T extends U?X:Y where T represents the type that will be checked, U – type to check against, X – type when true, Y – type if false.
type IsString<T> = T extends string ? "Yes" : "No";
30. What is a utility type in TypeScript?
Answer: These are predefined types that perform a standard type transformation like Partial, Required, Readonly, and Record.
type User = {
id: number;
name: string;
};
type PartialUser = Partial<User>; // All properties are optional
31. How to use the keyof operator?
Answer: The keyof operator creates a union type of the keys of an object type.
type User = {
id: number;
name: string;
};
type UserKeys = keyof User; // "id" | "name"
32. What is the ‘infer’ keyword in TypeScript?
Answer: The keyword ‘infer’ is used inside the conditional types to get the type variable coming from the type. This gives a way of capturing types in such a way that allows for higher flexibility.
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
33. How to create an Intersection Type?
Answer: Intersection types combine multiple types into one. You create an intersection type with the & operator.
type A = { x: number };
type B = { y: number };
type C = A & B; // { x: number; y: number }
34. What is the purpose of the void type?
Answer: The void type does not return any value. It is generally used in those kinds of functions which perform some actions and accordingly do not return anything.
function logMessage(message: string): void {
console.log(message);
}
35. How would you use the instanceof operator in TypeScript?
Answer: The instanceof operator checks whether an object is an instance of a class or constructor function and may be used for type narrowing.
class Animal {}
class Dog extends Animal {}
const dog = new Dog();
console.log(dog instanceof Dog); // true
console.log(dog instanceof Animal); // true
36. What is the keyword abstract used for?
Answer: Abstract Keyword is used to declare abstract classes and methods. An abstract class cannot be instantiated directly, and all its abstract methods are supposed to be implemented by its derived class.
abstract class Shape {
abstract area(): number;
}
class Circle extends Shape {
constructor(private radius: number) {
super();
}
area() {
return Math.PI * this.radius ** 2;
}
}
37. In TypeScript, how would you write a type guard?
Answer: A type guard is mainly a function that ensures the types of a variable in specific instances during runtime. Yes, you could narrow types using user-defined type guards.
function isString(value: any): value is string {
return typeof value === "string";
}
38. What keyword export serves for?
Answer: The keyword export enables the importation of variables, functions, or classes in another module. It facilitates modular organization of your code more easily .
export const PI = 3.14;
export function calculateArea(radius: number): number {
return PI * radius * radius;
}
39. What about the keyword import?
Answer: Import is a keyword used to import the exported members of other modules. Import can be done for single members or the entirety of the modules.
import { calculateArea, PI } from './math';
40. What does the declare keyword do?
Answer: The declare keyword in TypeScript is used to declare types or variables that exist in the global scope or provided by external libraries. It simply tells TypeScript that these things exist and it doesn’t provide their implementation.
declare const myGlobalVar: string;
41. How do you declare a type alias in TypeScript?
Answer: In programming, an alias gives another name for an existing type. A type alias is declared by using the keyword type:.
type StringOrNumber = string | number;
42. What is the purpose of the operator typeof?
Answer: The typeof operator returns the type of a variable or expression at runtime. It can be used in type annotations to refer to the type of a variable or function.
let age = 30;
type AgeType = typeof age; // AgeType is number
43. How would you, in TypeScript, declare a constant or read-only array?
Answer: You could declare a readonly array modifier: Once an array has been created, it is not allowed to modify it.
const numbers: readonly number[] = [1, 2, 3];
// numbers[0] = 4; // Error: Index signature in type 'readonly number[]' only permits reading
44. What is the purpose of the keyword asserts?
Answer: The asserts keyword in function signatures indicates that the function will assert a certain type. It’s useful for composing custom type guards.
function assertIsString(value: any): asserts value is string {
if (typeof value !== "string") {
throw new Error("Value is not a string");
}
}
45. How do you use the Promise type in TypeScript?
Answer: Promise would be the type for indicating asynchronous operations. You can, if you wish, declare the type of the value the promise resolves to.
function fetchData(): Promise<string> {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Data received");
}, 1000);
});
}
46. What is the async keyword used for?
Answer: Async is a keyword used to declare an asynchronous function. This means that it makes it possible to use await inside this function to easily handle promises.
async function getData() {
const data = await fetchData();
console.log(data);
}
47. How would you declare a custom type using an interface?
Answer: One could declare a type by defining what an object might look like using the ‘interface’ keyword, which could contain properties and methods.
interface Car {
make: string;
model: string;
year: number;
drive(): void;
}
48. What is the purpose of never type in function return types?
Answer: The never type is used to indicate that a function never returns a value-usually because it always throws an error or has an endless loop.
function throwError(message: string): never {
throw new Error(message);
}
49. How would you implement the modifier readonly with an interface?
Answer: One of the usages of the readonly modifier is to make properties in an interface immutable after they have been set once.
interface User {
readonly id: number;
name: string;
}
50. What is the purpose of the this parameter in TypeScript?
ANSWER: The this parameter identifies what type of this a function is operating on. It helps to make sure one uses the right context when methods are called.
function logThis(this: { name: string }) {
console.log(this.name);
}
Top 50 TypeScript interview questions and answers for 2025. Your studying this area will afford you an opportunity-to manifest your knowledge and skills in TypeScript. Good luck!