In this Blog we will discuss about top 10 interview Questions in Typescript.
1. What is TypeScript and how does it differ from JavaScript?
TypeScript is a subset of JavaScript that can be translated into standard JavaScript. From a conceptual standpoint, TypeScript and JavaScript have a similar relationship to SASS and CSS. In other words, TypeScript is the ES6 version of JavaScript with some added features.
JavaScript is a scripting language that is more comparable to Python than TypeScript, which is an object-oriented, statically typed language like Java and C#. Classes and interfaces are all part of TypeScript’s object-oriented design, and type inference is one of the many tools available thanks to its static typing.
2. What are the benefits of using TypeScript?
TypeScript is a strongly typed programming language that employs type inferences. These features pave the way for improved tooling and tighter integration with code editors. TypeScript’s strict checks detect errors early on. From the standpoint of an IDE, TypeScript allows your IDE to better understand your code, allowing it to display better hints, warnings, and errors to the developer. TypeScript’s strict null check, for example, throws an error at compile time (and in your IDE), preventing the common JavaScript error of attempting to access a property of an undefined variable at runtime.
3. What are interfaces in TypeScript?
Interfaces are TypeScript’s way of defining entity syntax. In other words, interfaces are a way of describing data shapes like objects or arrays of objects.
Interfaces aid in the maintenance of consistency in your TypeScript project. Furthermore, interfaces improve the tooling of your project by providing better autocomplete functionality in your IDEs and ensuring that the correct values are passed into constructors and functions.
4. How do you create a new type using a subset of an interface?
By providing a current type or interface and choosing the keys to be excluded from the new type, the ignore utility type in TypeScript enables you to create new types.
5. How do “enums” work in TypeScript?
A method of defining a collection of named constants is to use enums or enumerated types. These data structures have a fixed length and a collection of fixed values. For representing a set of options for a given value in TypeScript, enums are frequently used to describe the possibilities using a set of key/value pairs.
6. What are arrow functions in TypeScript?
Function declaration can be done quickly and easily with the help of arrow functions, also referred to as lambda functions. In TypeScript, callback functions are frequently written using arrow functions. All array operations that take arrow functions as arguments include map, filter, and reduce.
7. What are the differences between var, let, and const?
Var: Declares a global or function-scoped variable with JavaScript’s var variables’ behaviour and scoping restrictions. Var variables don’t need to have their values set when they are declared.
Let: Declares a local variable with a block scope. Let variables don’t require setting a variable’s value when they are declared. A local variable that is block-scoped can only be accessed from within the block in which it is contained, such as a function, an if/else block, or a loop. Let variables, as contrast to var, cannot be read from or written to before they are defined.
Const : Declares a value for a block-scoped constant that cannot be altered once it has been initialised. Const variables need to be initialised before being declared. For variables that remain constant during their entire existence, this is optimal.
8. What are conditional types in TypeScript?
Conditional types in TypeScript are similar to ternary operators. As the name suggests, it assigns a type to the variable based on a condition.
9. What access modifiers are supported by TypeScript?
The concept of “encapsulation” is used in object-oriented programming to control the visibility of its properties and methods. TypeScript uses access modifiers to set the visibility of a class’s contents. Because TypeScript gets compiled to JavaScript, logic related to access modifiers is applied during compile time, not at run time.
There are three types of access modifiers in TypeScript
1. public
2. private
3. protected.
1.Public : By default, all properties and methods are public. Members of a class who are public are viewable and reachable from anywhere.
2.Private : Private properties are only accessible from within the class the property or method is defined.
3.Protected : Access to protected properties is restricted to the parent class and its subclass. For instance, a variable or method that has the protected keyword applied to it will be available from anywhere both within the class in which it is contained and within another class that extends it.
10. What are generics and how to use them in TypeScript?
Reusability and flexibility are frequently encouraged by good software engineering practises. By allowing a component to function over a number of types rather than just one while maintaining its precision, the use of generics promotes flexibility and reusability (unlike the use of any).
For any Help or Queries Contact us on info@crmonce.com or +918096556344