TypeScript – Interview Questions & Answers

image 2

Play Store Application link – Java to TypeScript in 14 Steps – App on Google Play

  1. What is TypeScript and how is it different from JavaScript?
    TypeScript is a superset of JavaScript that adds optional static typing and other features, such as interfaces and classes. TypeScript allows developers to catch errors early and write more maintainable code than plain JavaScript.
  2. What are the benefits of using TypeScript?
    TypeScript offers several benefits, including improved code maintainability, better code documentation, stronger typing, and better tooling support. It also allows developers to catch errors early and improve overall code quality.
  3. How do you define a variable in TypeScript?
    To define a variable in TypeScript, use the let or const keyword followed by the variable name and its type annotation, like let myVar: string = "Hello World";.
  4. What is a type in TypeScript?
    In TypeScript, a type is a set of values that a variable can hold. Types can be built-in, such as string, number, and boolean, or user-defined, such as interfaces and classes.
  5. What is an interface in TypeScript?
    An interface in TypeScript is a way to define a contract that an object must adhere to. It specifies the properties and methods that an object must have, but doesn’t provide any implementation details.
  6. What is a class in TypeScript?
    A class in TypeScript is a blueprint for creating objects that share the same properties and methods. It encapsulates data and behavior and allows for code reuse and modularity.
  7. How do you define a function in TypeScript?
    To define a function in TypeScript, use the function keyword followed by the function name, parameters, and return type, like function add(a: number, b: number): number { return a + b; }.
  8. What is a module in TypeScript?
    A module in TypeScript is a way to organize code into reusable units. It allows developers to encapsulate code and share it across multiple files and projects.
  9. What is the difference between a namespace and a module in TypeScript?
    A namespace in TypeScript is a way to organize code by creating a global object that contains other objects and functions. A module, on the other hand, is a way to organize code into reusable units that can be imported and used by other modules.
  10. What is a decorator in TypeScript?
    A decorator in TypeScript is a way to add metadata to a class, method, or property. It allows developers to modify the behavior of a class or its members at runtime.
  11. How do you declare an array in TypeScript?
    To declare an array in TypeScript, use the [] notation after the variable name and specify the type of the array elements, like let myArray: number[] = [1, 2, 3];.
  12. What is a tuple in TypeScript?
    A tuple in TypeScript is an array that has a fixed number of elements, each with a different type. Tuples allow developers to specify the exact type and order of the elements in an array.
  13. What is a union type in TypeScript?
    A union type in TypeScript allows a variable to have multiple types. It is specified by using the | symbol between the types, like let myVar: string | number = "Hello World";.
  14. What is an intersection type in TypeScript?
    An intersection type in TypeScript allows a variable to have multiple types that are combined into a single type. It is specified by using the & symbol between the types, like let myVar: MyInterface & MyOtherInterface = { prop1: "hello", prop2: 42 };.
  15. What is an enum in TypeScript?
    An enum in TypeScript is a way to define a set of named constants. It allows developers to create a type with a limited number of values.
  16. What is type assertion in TypeScript?
    Type assertion in TypeScript is a way to tell the compiler the type of a variable, even if the compiler cannot infer it. It is specified by using the < > syntax or the as keyword, like let myVar: any = "Hello World"; let myStr: string = myVar as string;.
  17. What is the difference between “interface” and “type” in TypeScript?
    Interfaces and types are similar in that they both allow developers to define custom types in TypeScript. However, interfaces are more commonly used to describe object shapes, while types can be used for any type definition.
  18. What is a generic in TypeScript?
    A generic in TypeScript allows developers to write functions and classes that work with a variety of data types. It is specified by using angle brackets < > and a placeholder name for the data type, like function identity<T>(arg: T): T { return arg; }.
  19. What is a namespace in TypeScript?
    A namespace in TypeScript is a way to group related code and prevent naming collisions with code outside the namespace. It is specified by using the namespace keyword and can contain classes, interfaces, functions, and variables.
  20. How do you use “async” and “await” in TypeScript?
    To use async and await in TypeScript, mark the function with the async keyword and use the await keyword to wait for the result of an asynchronous operation. For example, async function getData() { const data = await fetch(url); return data.json(); }.
  21. What is the “never” type in TypeScript?
    The never type in TypeScript is used to represent a value that never occurs. It is often used in error handling and as a return type for functions that always throw an error or never return.
  22. What is the “readonly” keyword in TypeScript?
    The readonly keyword in TypeScript is used to make a property or variable immutable. Once set, a readonly property or variable cannot be changed.
  23. What is the “keyof” operator in TypeScript?
    The keyof operator in TypeScript is used to get the keys of an object as a union type. It is often used in combination with generics and interfaces to create more flexible code.
  24. What is the difference between “require” and “import” in TypeScript?
    require is a commonJS syntax for importing modules in Node.js, while import is a ECMAScript syntax used in modern JavaScript and TypeScript. import also offers additional features, such as named imports and exports.
  25. What is the difference between “class” and “interface” in TypeScript?
    A class in TypeScript is used to create objects with methods and properties, while an interface is used to describe the shape of an object without providing any implementation details. However, a class can implement an interface, meaning it must have the properties and methods defined in the interface.

Leave a Reply

Your email address will not be published. Required fields are marked *