Top Typescript Interview Questions (2023) | CodeUsingJava

Top Typescript Questions

In this post we will look at Typescript Interview questions. Examples are provided with explanation.
Typescript Interview Questions

What is Typescript?

  • TypeScript is an programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language it is designed for development of large applications and transcompiles to JavaScript.
  • TypeScript may be used to develop JavaScript applications for both client-side and server-side execution. There are multiple options available for transcompilation. Either the default TypeScript Checker can be used,or the Babel compiler can be invoked to convert TypeScript to JavaScript. ]

What are the advantages of using Typescript?

  • Code scalability with "Interface oriented development"
  • TypeScript helps you dealing with teams.For example, Microsoft made an awesome community effort work.
  • Refactoring by typescript tools are faster and easier.
  • Types have a proven ability to enhance code quality and understandability.
  • Types increase your agility when doing refactoring. It is better for the compiler to catch errors than to have things fail at runtime.
  • Types are one of the best forms of documentation you can have. The function signature is a theorem and the function body is the proof.
  • TypeScript give you a taste of future JavaScript, with typing.

What can we convert string to number using Typescript?

In Typescript this can be achieved in 4 ways
  • Using parseInt
  • Using parseFloat
  • Using Unary like +,* operator
    var a = "31";
    var b: number = +a;
  • Using Number

What is difference between Constructor and ngOnInit in Typescript?

In Typescript the constructor is a default method of the class . It gets executed when the class is instantiated. It is part of ES6 JavaScript
new testClass(args);
ngOnInit is a life cycle hook called by Angular to indicate that Angular is done creating the component.

What is Decision Making in Typescript?

Decision-making structures required so that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Typescript Decision Making
A decision-making construct evaluates a condition before the instructions are executed.

Why would we use Typescript in place of JavaScript?

  • JavaScript is a scripting language which helps you create interactive web pages whereas Typescript is a superset of JavaScript.
  • Typescript code needs to be compiled while JavaScript code does not need to compile.
  • Typescript supports a feature of prototyping while JavaScript doesn't support this feature.
  • Typescript uses concepts like types and interfaces to describe data being used whereas JavaScript has no such concept.
  • Typescript is a powerful type system, including generics & JS features for large size project whereas JavaScript is an ideal option for small size project.

What is Basic Syntax in Typescript?

Syntax defines a set of rules for writing programs. Every language specification defines its own syntax.
Your First TypeScript Code
Let us start with the traditional "People" example −

var message:string = "People"

What is a TypeScript Map file?

Map files are source map files that let tools map between the emitted JavaScript code and the TypeScript source files that created it. Many debuggers can consume these files so you can debug the TypeScript file instead of the JavaScript file.
For eg. Visual Studio or Chrome's dev tools)

What are different components of TypeScript?

  • Language – The most important part for developers is the new language. The language consist of new syntax, keywords and allows you to write TypeScript.
  • Compiler – The TypeScript compiler is open source, cross-platform and open specification, and is written in TypeScript. Compiler will compile your TypeScript into JavaScript. And it will also emit error, if any. It can also help in concating different files to single output file and in generating source maps.
  • Language Service – TypeScript language service which powers the interactive TypeScript experience in Visual Studio, VS Code, Sublime, the TypeScript playground and other editor.

What are Ambients in TypeScript?

  • Ambient declarations are a way of telling the TypeScript compiler that the actual source code exists elsewhere.
  • When you are consuming a bunch of third party js libraries like jquery/angularjs/nodejs you can’t rewrite it in TypeScript. Ensuring typesafety and intellisense while using these libraries will be challenging for a TypeScript programmer. Ambient declarations help to seamlessly integrate other js libraries into TypeScript.