Typescript Interview Questions

Typescript Interview Questions Download Typescript Interview Questions PDF

Typescript Interview Questions

Typescript is a free and open-source programming language which is designed and developed by Microsoft. It was designed by Anders Hejlsberg at Microsoft. Typescript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. Typescript is compiled to provide clean and simple JavaScript code which runs on any browser. It allows JavaScript developers for using highly productive development tools and practices like static checking and code refactoring.

Differences between Typescript and JavaScript are

  1. JavaScript don’t support Es6 while Typescript supports .
  2. JavaScript build up reusable components by using unctions and prototype-based inheritance while Typescript supports Classes that allow programmer to think in more object oriented way .
  3. JavaScript don’t have any interfaces while Typescript has interfaces.
  4. There is no static typing in JavaScript whereas there is static typing in Typescript.
  5. JavaScript has no optional parameter  feature while Typescript has optional parameter feature.
Modules are powerful way to share code between files.By using modules effectively in your project you can keep you file size small and dependencies clear.

Modules are executed within their own scope, not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms.

Creating a Module

module module_name{

	class xyz{
		export foo(x, y){
		 	return x*y;
		 }
	}

}

Decorators are function that supports annotating or modifying classes its members.Its allow way to add both annotations and a meta-programming syntax for class declarations and members. Decorators are an experimental feature of Typescript and maybe change in future releases.

You can enable Decorators using the command line or editing your tsconfig.json

Enabling Decorators in TypeScript via command line

tsc --target ES5 --experimentalDecorators
A variable is a named space in the memory which stores a particular value. While declaring a variable in Typescript, certain rules should be followed like-
  1. A variable name should contain alphabets and numeric digits.
  2. It cannot contain spaces and special characters except underscore (_) and dollar ($) sign.
  3. A variable name cannot begin with a digit.

You should always keep in mind, that a variable should be declared before being used. Variables in Typescript are declared by placing a “var” keyword prior to the variable name. A variable can be declared using 4 methods: –

  • Declare its type and value in one statement.Syntax- var variable_name:string = value;
  • Declare its type but no value.Syntax- var variable_name:string;
  • Declare its value but no type.Syntax- var variable_name = value;
  • Declare neither value nor type.Syntax- var variable_name;
Using below command we can compile multiple typescript files into a single file
tsc --outFile outputfile.ts file1.ts file2.ts file3.ts  ... filen.ts