Interview questions for Typescript
1) What is Typescript?
2. Why do we need TypeScript?
We need TypeScript:
o TypeScript is fast, simple, and most importantly, easy to learn.
o TypeScript supports object-oriented programming features such as classes, interfaces, inheritance, generics, etc.
o TypeScript provides the error-checking feature at compilation time. It will compile the code, and if any error found, then it highlighted the errors before the script is run.
o TypeScript support reusability by using the inheritance.
o TypeScript make app development quick and easy as possible, and the tooling support of TypeScript gives us autocompletion, type checking, and source documentation.
o TypeScript gives all the benefits of ES6 plus more productivity.
o TypeScript supports Static typing, Strongly type, Modules, Optional Parameters, etc.
4. What are the benefits of using TypeScript?
Each programming language has its pros and cons and the reasons behind its applications. An interviewer might ask this question to gain an insight into the candidate’s personal experience with using TypeScript and how it has affected the developer experience and the project’s workflow.
Instead of just listing out TypeScript’s benefits, also talk about how a typical project could benefit from TypeScript. This could range from better maintainability in the long run or increased developer productivity because of the features that TypeScript offers.
A long-run advantage of using TypeScript is its scalability and maintainability. The ability to describe the shape of objects and functions directly in your code makes your codebase easier to understand and more predictable. When used correctly, TypeScript provides a more standardized language resulting in better readability which could save time and effort down the road as the codebase grows.
5. What are the variables in Typescript? How to create a variable in Typescript?
A variable is the storage location, which is used to store value/information to be referenced and used by programs. It acts as a container for value in a program. It can be declared using the var keyword. It should be declared before the use. While declaring a variable in Typescript, certain rules should be followed-
o The variable name must be an alphabet or numeric digits.
o The variable name cannot start with digits.
o The variable name cannot contain spaces and special character, except the underscore(_) and the dollar($) sign.
We can declare a variable in one of the four ways:
1. Declare type and value in a single statement. Syntax: var [identifier] : [type-annotation] = value;
2. Declare type without value. Syntax: var [identifier] : [type-annotation];
3. Declare its value without type. Syntax: var [identifier] = value;
4. Declare without value and type. Syntax: var [identifier];
What is the difference between union and intersection types?
Union and intersection types are considered as advanced types in Typescript. Understanding this is important when creating types in your Typescript projects to reduce duplication of types. The candidate should ideally be able to explain what are union and intersection types, their differences, and their use cases.
Unions and intersection types let you compose and combine existing types instead of creating them from scratch. Both union and intersection come with their unique characteristics which make them ideal for different use cases.
A union type is described as a type that can be one of several types. Union type uses the | (vertical bar) symbol to separate the list of types that will be used in the new type.