Most targeted up-to-date TypeScript interview questions and answers list
- What is TypeScript?
- How do you define a variable with a specific type in TypeScript?
- What is the difference between "interface" and "type" in TypeScript?
- How do you define a function with optional parameters in TypeScript?
- How do you define an interface in TypeScript?
- What are generics in TypeScript? Provide an example.
- How do you declare an array in TypeScript?
- Explain the concept of classes in TypeScript. Provide an example.
- How do you import and export modules in TypeScript? Provide an example.
- What are type assertions in TypeScript? Provide an example.
- What is the "this" keyword in TypeScript? Provide an example.
- How do you handle asynchronous operations in TypeScript? Provide an example using Promises.
TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It introduces static typing and provides additional features like interfaces, classes, and modules to JavaScript.
let age: number = 25; // Define a variable 'age' of type 'number' with value 25
In TypeScript, both "interface" and "type" can be used to define object shapes and types. However, "interface" is used more for object shapes, while "type" is used for creating aliases of other types or creating union types.
function greet(name: string, age?: number): void {
console.log(`Hello, ${name}! You are ${age || 'unknown'} years old.`);
}
greet('John'); // Output: Hello, John! You are unknown years old.
greet('Alice', 30); // Output: Hello, Alice! You are 30 years old.
interface Person {
name: string;
age: number;
}
const person: Person = {
name: 'John',
age: 25,
};
Generics allow you to create reusable components or functions that can work with a variety of types. Here's an example of a generic function that swaps two values:
function swap<T>(a: T, b: T): void {
let temp: T = a;
a = b;
b = temp;
}
let x: number = 5;
let y: number = 10;
swap<number>(x, y);
console.log(x, y); // Output: 10 5
let numbers: number[] = [1, 2, 3, 4, 5]; // Declare an array of numbers
Classes in TypeScript provide a way to define blueprints for creating objects. They encapsulate data and behavior into a single unit. Here's an example of a class representing a Person:
class Person {
constructor(public name: string, public age: number) {}
sayHello(): void {
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
}
}
const person = new Person('John', 25);
person.sayHello(); // Output: Hello, my name is John and I'm 25 years old.
// Example of exporting a module
export function greet(name: string): void {
console.log(`Hello, ${name}!`);
}
// Example of importing a module
import { greet } from './greetings';
greet('John'); // Output: Hello, John!
Type assertions allow you to explicitly specify the type of a value when the TypeScript compiler cannot infer it. Here's an example:
let message: any = 'Hello, world!';
let length: number = (message as string).length; // Type assertion to treat 'message' as a string
The "this" keyword refers to the current instance of an object within a method or function. Here's an example:
const person = {
name: 'John',
age: 25,
greet: function () {
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
},
};
person.greet(); // Output: Hello, my name is John and I'm 25 years old.
// Example of handling asynchronous operation with Promises
function fetchData(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Data fetched successfully!');
}, 2000);
});
}
fetchData()
.then((data) => {
console.log(data); // Output: Data fetched successfully!
})
.catch((error) => {
console.error(error);
});
A comprehensive list of questions and answers
We welcome contributions from our users to help make this resource as comprehensive and useful as possible. If you have been recently interviewed and encountered a question that is not currently covered on our website, feel free to suggest it as a new question. Your contributions will be added to our platform, and we will make sure to credit you for your contributions. We appreciate your help in making our platform a valuable tool for all job seekers.
MIT License