Skip to content

interviewplus-ai/typescript-interview-questions-and-answers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Typescript Interview Questions And Answers

Most targeted up-to-date TypeScript interview questions and answers list

Table of Contents

  1. What is TypeScript?
  2. How do you define a variable with a specific type in TypeScript?
  3. What is the difference between "interface" and "type" in TypeScript?
  4. How do you define a function with optional parameters in TypeScript?
  5. How do you define an interface in TypeScript?
  6. What are generics in TypeScript? Provide an example.
  7. How do you declare an array in TypeScript?
  8. Explain the concept of classes in TypeScript. Provide an example.
  9. How do you import and export modules in TypeScript? Provide an example.
  10. What are type assertions in TypeScript? Provide an example.
  11. What is the "this" keyword in TypeScript? Provide an example.
  12. How do you handle asynchronous operations in TypeScript? Provide an example using Promises.

1. What is TypeScript?

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.

2. How do you define a variable with a specific type in TypeScript?

let age: number = 25; // Define a variable 'age' of type 'number' with value 25

3. What is the difference between "interface" and "type" in TypeScript?

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.

4. How do you define a function with optional parameters in TypeScript?

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.

5. How do you define an interface in TypeScript?

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

const person: Person = {
  name: 'John',
  age: 25,
};

6. What are generics in TypeScript? Provide an example.

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

7. How do you declare an array in TypeScript?

let numbers: number[] = [1, 2, 3, 4, 5]; // Declare an array of numbers

8. Explain the concept of classes in TypeScript. Provide an example.

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.

9. How do you import and export modules in TypeScript? Provide an example.

// 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!

10. What are type assertions in TypeScript? Provide an example.

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

11. What is the "this" keyword in TypeScript? Provide an example.

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.

12. How do you handle asynchronous operations in TypeScript? Provide an example using Promises.

// 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);
  });

What's more?

A comprehensive list of questions and answers

Contributing

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.

License

MIT License

About

Most targeted up-to-date TypeScript interview questions and answers list

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published