Interview questions for Typescript

1) What is Typescript?

Typescript is a free and open-source programming language developed and maintained by Microsoft. It is a strongly typed superset of JavaScript that compiles to plain JavaScript. It is a language for application-scale JavaScript development. TypeScript is quite easy to learn and use for developers familiar with C#, Java and all strong typed languages.
TypeScript can be executed on Any browser, Any Host, and Any Operating System. TypeScript is not directly run on the browser. It needs a compiler to compile and generate in JavaScript file. TypeScript is the ES6 version of JavaScript with some additional features.

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 supports all JavaScript libraries because it is the superset of JavaScript.
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 supports the latest JavaScript features including ECMAScript 2015.
o TypeScript gives all the benefits of ES6 plus more productivity.
o TypeScript supports Static typing, Strongly type, Modules, Optional Parameters, etc.

3. What is TypeScript and how does it differ from JavaScript?

This is a fundamental question that an interviewer typically asks to gauge the candidate’s familiarity with the language and how comfortable they are with its features. As the candidate, make sure you can describe TypeScript as a language, its core features, and how it relates to JavaScript.
TypeScript is a superset of JavaScript that compiles to plain JavaScript. Conceptually, the relationship between TypeScript and JavaScript is comparable to that of SASS and CSS. In other words, TypeScript is JavaScript’s ES6 version with some additional features.
TypeScript is an object-oriented and statically typed language, similar to Java and C#, whereas JavaScript is a scripting language closer to Python. The object-oriented nature of TypeScript is complete with features such as classes and interfaces, and its static typing allows for better tooling with type inference at your disposal.
From a code perspective, TypeScript is written in a file with a .ts extension whereas JavaScript is written with a .js extension. Unlike JavaScript, TypeScript code is not understandable by the browsers and can’t be executed directly in the browser or any other platform. The .ts files need to be transpiled using TypeScript’s tsc transpiler to plain JavaScript first, which then gets executed by the target platform.

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.
An immediate advantage of using TypeScript is its tooling. TypeScript is a strongly typed language that uses type inferences. These characteristics open the doors to better tooling and tighter integration with code editors. TypeScript’s strict checks catch your errors early, greatly reducing the chances of typos and other human errors from making their way to production. From an IDE’s perspective, TypeScript provides the opportunity for your IDE to understand your code better allowing it to display better hints, warnings, and errors to the developer. For example, TypeScript’s strict null check throws an error at compile time (and in your IDE) preventing a common JavaScript error of attempting to access a property of an undefined variable at runtime.
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. 

leave your comment

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