Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Why TypeScript?

Answer»

TypeScript is an open-source language developed by Anders Hejlsberg at Microsoft. It’s a statically-typed superset of JavaScript that compiles to plain JavaScript. It runs on any browser, host, and operating system. That means all VALID JavaScript code is also TypeScript code. It offers advanced FEATURES such as IntelliSense, code completion, safe refactorings, etc.

As JavaScript projects grow in size, they become difficult to maintain. There are a few reasons for this. First, JavaScript was never designed to build large-scale applications. Its original purpose was to provide small scripting functionality for a web page. Until recently, it didn’t provide tools and constructs for structuring large projects, such as classes, modules, and interfaces. Also, JavaScript is dynamically typed. It doesn’t support features such as IntelliSense. 

TypeScript files use a .ts extension, in contrast to the .js extension used by the JavaScript files. Since TypeScript is a superset of JavaScript, all valid JavaScript code is a valid TypeScript code, and renaming a .js file to .ts won’t change ANYTHING. Here is an example of a standard TypeScript program that adds two numbers and returns the result. Notice how the arguments and the return types are annotated with their type.

function add(a: number, b: number): number { const sum = a + b; return sum;}

When you compile a TypeScript file using the tsc command, the Typescript compiler generates vanilla JavaScript that GETS executed. For example, this is what the compiler will produce for the above snippet of code.

function add(a, b) { const sum = a + b; return sum;}

TypeScript adds optional static typing and language features such as classes and modules. It’s IMPORTANT to know that all these advanced features add zero cost to JavaScript. A typeScript is purely a compile-time tool. Once you compile, you are left with plain, idiomatic JavaScript. TypeScript is a language for application scale JavaScript development.