Are you preparing for a TypeScript interview as a beginner or fresher? This guide covers the most commonly asked TypeScript interview questions and answers, starting from basic concepts and gradually moving toward practical and coding-based questions.
TypeScript is widely used in modern web development, Node.js applications, Angular, React projects, and test automation frameworks such as Playwright with TypeScript.
This article contains 60 TypeScript interview questions and answers covering variables, data types, interfaces, classes, generics, functions, type guards, TypeScript configuration, and practical coding questions.
Table of Contents
- What is TypeScript?
- JavaScript vs TypeScript
- TypeScript Data Types
- Interfaces
- Type Alias
- Union Types
- Classes and OOP
- Generics
- Other Important Concepts
- TypeScript Coding Questions
- Frequently Asked Questions
1. What is TypeScript?
Answer: TypeScript is a strongly typed programming language developed by Microsoft. It is a superset of JavaScript, which means valid JavaScript code can generally be used in a TypeScript project.
TypeScript adds several features to JavaScript, including:
- Static typing
- Interfaces
- Generics
- Enums
- Access modifiers
- Better IDE support
- Compile-time type checking
TypeScript code is transformed into JavaScript before it runs in a browser or Node.js environment.
2. What is the difference between JavaScript and TypeScript?
| JavaScript | TypeScript |
|---|---|
| Dynamically typed | Supports static typing |
Uses .js files |
Uses .ts files |
| Many type errors are discovered at runtime | Many type errors can be detected during development |
| Does not require TypeScript compilation | Usually transpiled to JavaScript |
| Less type information | Provides rich type information |
3. Why do we use TypeScript?
TypeScript makes JavaScript development safer and easier to maintain, particularly in large applications.
Major advantages include:
- Early error detection
- Better code completion
- Improved refactoring
- Static type checking
- Better maintainability
- Support for object-oriented programming
- Improved developer productivity
4. Is TypeScript a programming language?
Answer: Yes. TypeScript is a programming language developed and maintained by Microsoft. It extends JavaScript by adding static typing and other development features.
5. What is the TypeScript compiler?
The TypeScript compiler converts TypeScript source code into JavaScript.
The compiler command is commonly called tsc.
tsc app.ts
For example:
let message: string = "Hello TypeScript";
console.log(message);
The TypeScript compiler generates JavaScript that can execute in a JavaScript environment.
6. How do you install TypeScript?
First install Node.js and npm. Then install TypeScript using:
npm install -g typescript
Verify the installation:
tsc --version
7. What is a .ts file?
A .ts file is a TypeScript source file.
Examples:
app.ts
login.ts
user.ts
employee.ts
TypeScript code is normally written in .ts files and then compiled or transpiled into JavaScript.
8. What are the basic data types in TypeScript?
Some commonly used TypeScript types are:
string
number
boolean
array
tuple
enum
any
unknown
void
null
undefined
never
object
Example:
let name: string = "Deepesh";
let age: number = 35;
let isTrainer: boolean = true;
9. What is type annotation?
Type annotation means explicitly specifying the type of a variable.
let username: string = "admin";
let age: number = 30;
let active: boolean = true;
Here, string, number, and boolean are type annotations.
10. What is type inference?
Type inference means TypeScript automatically determines the type of a variable based on its assigned value.
let name = "Deepesh";
let age = 30;
TypeScript infers:
name: string
age: number
11. What is the any type?
The any type disables most TypeScript type checking for a value.
let data: any = "Hello";
data = 100;
data = true;
data = { name: "John" };
Although any can be useful when migrating JavaScript code or dealing with genuinely untyped APIs, excessive use of it removes many of the benefits of TypeScript.
12. What is the unknown type?
The unknown type is used when the type of a value is not known.
let value: unknown = "Hello";
if (typeof value === "string") {
console.log(value.toUpperCase());
}
Unlike any, TypeScript requires you to perform an appropriate type check before using an unknown value in type-specific operations.
13. What is an array in TypeScript?
An array stores multiple values.
let numbers: number[] = [10, 20, 30];
let names: string[] = ["John", "David", "Alex"];
Another syntax is:
let numbers: Array<number> = [10, 20, 30];
14. What is a tuple?
A tuple defines a fixed structure for an array, including the expected type and position of its elements.
let employee: [number, string] = [101, "John"];
In this example:
- The first element must be a number.
- The second element must be a string.
15. What is an interface?
An interface defines the structure or contract of an object.
interface Employee {
id: number;
name: string;
salary: number;
}
const employee: Employee = {
id: 101,
name: "John",
salary: 50000
};
Interfaces are commonly used to define the expected structure of objects and contracts implemented by classes.
16. What is an optional property?
A property followed by ? is optional.
interface Employee {
id: number;
name: string;
email?: string;
}
Therefore, the following object is valid:
const employee: Employee = {
id: 101,
name: "John"
};
17. What is a type alias?
A type alias allows you to create a custom name for a type.
type Employee = {
id: number;
name: string;
};
const emp: Employee = {
id: 101,
name: "John"
};
Type aliases can also represent unions and other type expressions.
type ID = string | number;
let userId: ID = 101;
userId = "EMP101";
18. What is the difference between interface and type?
Both interfaces and type aliases can describe object structures.
interface User {
name: string;
}
Equivalent object type:
type User = {
name: string;
};
Interfaces are especially useful for object/class contracts and can be extended. Type aliases are more flexible for unions, intersections, tuples, and other type expressions.
19. What is a union type?
A union type allows a variable to have more than one possible type.
let id: string | number;
id = 101;
id = "EMP101";
The | symbol represents a union.
20. What is an intersection type?
An intersection type combines multiple types into a single type.
type Person = {
name: string;
};
type Employee = {
salary: number;
};
type EmployeeDetails = Person & Employee;
const emp: EmployeeDetails = {
name: "John",
salary: 50000
};
The & operator represents an intersection type.
21. What is an enum?
An enum allows you to define a collection of named constants.
enum Status {
Pending,
Approved,
Rejected
}
let currentStatus: Status = Status.Approved;
Enums can be useful when working with a predefined set of values.
22. How do you define a function in TypeScript?
function add(a: number, b: number): number {
return a + b;
}
console.log(add(10, 20));
The parameter types are defined using : number, and the return type is also specified as number.
23. What is an optional parameter?
An optional parameter is defined using ?.
function greet(name: string, age?: number) {
console.log(name);
}
greet("John");
greet("John", 30);
The age parameter is optional.
24. What are default parameters?
A default parameter receives a default value when no value is provided.
function greet(name: string = "Guest") {
console.log(`Hello ${name}`);
}
greet();
Output:
Hello Guest
25. What is a class in TypeScript?
A class is a blueprint for creating objects.
class Employee {
name: string;
constructor(name: string) {
this.name = name;
}
display() {
console.log(this.name);
}
}
const emp = new Employee("John");
emp.display();
TypeScript classes support object-oriented programming concepts such as encapsulation, inheritance, abstraction, and polymorphism.
26. What are access modifiers in TypeScript?
TypeScript supports the following common access modifiers:
- public – accessible from anywhere
- private – accessible only inside the declaring class
- protected – accessible inside the class and subclasses
class Employee {
public name: string;
private salary: number;
protected department: string;
constructor(
name: string,
salary: number,
department: string
) {
this.name = name;
this.salary = salary;
this.department = department;
}
}
27. What is inheritance?
Inheritance allows one class to reuse members of another class.
class Animal {
eat() {
console.log("Eating");
}
}
class Dog extends Animal {
bark() {
console.log("Barking");
}
}
const dog = new Dog();
dog.eat();
dog.bark();
Here, Dog inherits from Animal.
28. What are generics in TypeScript?
Generics allow you to create reusable code that works with different types while preserving type safety.
function identity<T>(value: T): T {
return value;
}
let numberValue = identity<number>(100);
let stringValue = identity<string>("Hello");
Here, T acts as a type parameter.
29. What is type assertion?
Type assertion tells TypeScript that you know more about the type of a value than the compiler currently does.
let value: unknown = "Hello";
let message = value as string;
console.log(message.toUpperCase());
The as syntax is commonly used for type assertions.
30. What is void?
The void type is commonly used for functions that do not return a meaningful value.
function printMessage(): void {
console.log("Hello");
}
31. What is null and undefined?
undefined generally means a value has not been assigned.
let name: string | undefined;
console.log(name);
null represents an intentional absence of a value.
let user: string | null = null;
32. What is never in TypeScript?
never represents a value that never occurs. It is commonly used for functions that never successfully complete.
function throwError(message: string): never {
throw new Error(message);
}
33. What is strict mode?
Strict mode enables a collection of stronger type-checking rules.
{
"compilerOptions": {
"strict": true
}
}
Strict checking helps identify potential problems during development.
34. What is tsconfig.json?
tsconfig.json is the configuration file for a TypeScript project.
Example:
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"strict": true,
"outDir": "./dist"
},
"include": [
"src/**/*.ts"
]
}
35. What is the target option?
The target option specifies the JavaScript version that TypeScript should generate.
{
"compilerOptions": {
"target": "ES2022"
}
}
36. What is the module option?
The module option controls the module system used by the generated JavaScript.
{
"compilerOptions": {
"module": "commonjs"
}
}
Modern projects may use module settings such as ESNext depending on the runtime and build system.
37. What is type narrowing?
Type narrowing means reducing a broad type to a more specific type based on a condition.
function printValue(value: string | number) {
if (typeof value === "string") {
console.log(value.toUpperCase());
} else {
console.log(value.toFixed(2));
}
}
38. What is typeof used for?
The typeof operator can be used to check the runtime type of a value and can also help TypeScript narrow a union type.
function display(value: string | number) {
if (typeof value === "string") {
console.log("String:", value);
} else {
console.log("Number:", value);
}
}
39. What is a type guard?
A type guard is a condition or function that helps TypeScript determine the specific type of a value.
function isString(value: unknown): value is string {
return typeof value === "string";
}
const data: unknown = "Hello";
if (isString(data)) {
console.log(data.toUpperCase());
}
40. What is instanceof?
The instanceof operator checks whether an object is an instance of a particular class or constructor.
class Employee {
}
const emp = new Employee();
console.log(emp instanceof Employee);
Output:
true
41. What is optional chaining?
Optional chaining ?. allows you to safely access properties that may not exist.
interface User {
name: string;
address?: {
city: string;
};
}
const user: User = {
name: "John"
};
console.log(user.address?.city);
42. What is the nullish coalescing operator?
The ?? operator provides a fallback value when the left-hand side is null or undefined.
let username: string | undefined;
let result = username ?? "Guest";
console.log(result);
Output:
Guest
43. What is readonly?
The readonly modifier prevents a property from being assigned a new value after initialization.
interface Employee {
readonly id: number;
name: string;
}
const employee: Employee = {
id: 101,
name: "John"
};
employee.name = "David";
// Error:
// employee.id = 102;
44. What is the difference between const and readonly?
const applies to a variable binding, while readonly applies to a property or type member.
const employee = {
name: "John"
};
employee.name = "David";
The object reference cannot be reassigned, but its property can still be changed.
With readonly:
interface Employee {
readonly id: number;
}
The id property cannot be reassigned after initialization.
45. Can an interface extend another interface?
Yes. An interface can extend another interface.
interface Person {
name: string;
}
interface Employee extends Person {
employeeId: number;
}
const emp: Employee = {
name: "John",
employeeId: 101
};
46. Can a class implement an interface?
Yes. A class can implement one or more interfaces.
interface Employee {
name: string;
display(): void;
}
class Manager implements Employee {
name: string;
constructor(name: string) {
this.name = name;
}
display(): void {
console.log(this.name);
}
}
47. What is the difference between extends and implements?
extends is generally used for inheritance.
class Dog extends Animal {
}
implements is used when a class agrees to follow an interface contract.
class Employee implements Person {
}
48. Can an interface have methods?
Yes. An interface can define method signatures.
interface Employee {
name: string;
display(): void;
}
A class implementing the interface must provide the required method.
49. What is function overloading?
Function overloading allows you to define multiple function signatures for different valid calling patterns.
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: number | string, b: number | string) {
return (a as any) + (b as any);
}
console.log(add(10, 20));
console.log(add("Hello ", "World"));
50. What is the purpose of generics?
Generics allow developers to create reusable, type-safe functions, classes, and interfaces.
function getValue<T>(value: T): T {
return value;
}
const numberValue = getValue(100);
const stringValue = getValue("Hello");
const booleanValue = getValue(true);
TypeScript Coding Interview Questions
51. Write a function to calculate the sum of two numbers.
function sum(a: number, b: number): number {
return a + b;
}
console.log(sum(10, 20));
52. Create an interface for a Student.
interface Student {
id: number;
name: string;
age: number;
}
const student: Student = {
id: 1,
name: "John",
age: 22
};
53. Create an interface with an optional property.
interface User {
id: number;
name: string;
email?: string;
}
54. Create a function that accepts string or number.
function display(value: string | number): void {
console.log(value);
}
display("Hello");
display(100);
55. Create a generic function.
function getData<T>(data: T): T {
return data;
}
console.log(getData<number>(100));
console.log(getData<string>("TypeScript"));
Practical TypeScript Interview Questions
56. What will be the output?
let value: string | number = "100";
if (typeof value === "string") {
console.log("String");
} else {
console.log("Number");
}
Answer:
String
Because the value is initially assigned a string.
57. Is this code valid?
let age: number = 25;
age = "25";
Answer: No.
TypeScript reports a type error because age was declared as a number.
58. Is this valid TypeScript?
let data: any = 100;
data = "Hello";
data = true;
Answer: Yes.
The any type allows the variable to contain values of different types. However, it should be used carefully.
59. What is the output?
let username: string | undefined;
console.log(username ?? "Guest");
Answer:
Guest
Because username is undefined.
60. What is the main advantage of generics?
Generics allow you to write reusable code while preserving type information.
function getData<T>(data: T): T {
return data;
}
const numberData = getData(100);
const stringData = getData("Hello");
TypeScript can infer the appropriate type for T.
Important TypeScript Interview Topics for Beginners
If you have limited preparation time, focus on the following topics:
- What is TypeScript?
- TypeScript vs JavaScript
- Advantages of TypeScript
- Type annotations
- Type inference
- any vs unknown
- Union types
- Intersection types
- Interfaces
- Type aliases
- Interface vs type
- Arrays
- Tuples
- Enums
- Functions
- Optional parameters
- Classes
- Access modifiers
- Inheritance
- extends vs implements
- Generics
- Type assertions
- Type guards
- null and undefined
- never
- void
- readonly
- Optional chaining
- Nullish coalescing
- tsconfig.json
TypeScript Interview Preparation Tips
For a beginner TypeScript interview, don’t just memorize definitions. Try to understand each concept with a small coding example.
- Understand JavaScript fundamentals first.
- Practice TypeScript data types.
- Write interfaces for real-world objects.
- Practice classes and inheritance.
- Understand union and intersection types.
- Practice generics with simple examples.
- Learn type narrowing and type guards.
- Understand
any,unknown,never, andvoid. - Practice TypeScript coding questions.
- Learn how
tsconfig.jsonworks.
TypeScript for Playwright Automation
TypeScript is particularly useful for test automation because it provides type safety, better autocomplete, and improved maintainability.
For example, a Playwright test can be written as:
import { test, expect } from '@playwright/test';
test('Login test', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle(/Example/);
});
Understanding TypeScript fundamentals is therefore highly recommended if you are preparing for a Playwright with TypeScript or SDET interview.
Frequently Asked Questions
Is TypeScript difficult for beginners?
No. If you already know JavaScript fundamentals, TypeScript can be learned progressively by adding types and understanding interfaces, classes, generics, and other features.
Do I need to learn JavaScript before TypeScript?
Yes, it is highly recommended. TypeScript builds on JavaScript, so understanding variables, functions, arrays, objects, classes, promises, and asynchronous programming will make TypeScript much easier to learn.
Is TypeScript used in automation testing?
Yes. TypeScript is widely used with modern automation frameworks, including Playwright. It provides type checking, IDE support, and improved maintainability for automation frameworks.
What should I learn after TypeScript basics?
After learning the fundamentals, you should study advanced TypeScript concepts such as generics, utility types, mapped types, conditional types, decorators, modules, namespaces, advanced type narrowing, and TypeScript configuration.
Conclusion
TypeScript is an important skill for modern web development and test automation. Beginners should first understand the fundamentals such as data types, type annotations, type inference, interfaces, type aliases, functions, classes, union types, and generics.
Once these concepts are clear, you can move toward advanced TypeScript concepts and frameworks such as Playwright, Angular, React, Node.js, and automation framework development.
Practicing the questions and coding examples in this article will help you build a strong foundation for TypeScript interviews.
Next Recommended Topic: TypeScript Interview Questions and Answers for Intermediate and Advanced Developers.