TypeScript
- Sep 28, 2016
- 2 min read
TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language.
TypeScript may be used to develop JavaScript applications for client-side or server-side (Node.js) execution. TypeScript supports definition files that can contain type information of existing JavaScript libraries, The TypeScript compiler is itself written in TypeScript, transcompiled to JavaScript and licensed under the Apache 2 License.
Let us discuss some of the commonly asked interview questions about TypeScript.
1.What is TypeScript? -TypeScript is a free and open source programming language developed and maintained by Microsoft. -It is a strict superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language. -TypeScript is a language that generates plain JavaScript files.
2.What are the Advantage of TypeScript? -helps in code structuring -Use class based object oriented programming -Compile time error checking -Offers type checking
3.Which are the Different Data Types Supported by TypeScript? Boolean
var bValue: boolean = false; Number
var age: number = 16; String
var name: string = "jon"; Array
var list:number[] = [1, 2, 3]; Enum enum Color {Red, Green, Blue}; var c: Color = Color.Green; Any
var unknownType: any = 4; Void function NoReturnType(): void { }
4.What are the Disadvantage of TypeScript? - To run the application in the browser, a compile step is required to transform TypeScript into JavaScript. -To use any third party library, definition file is you need. And not all the third party library have definition file available.
5.How Can You Get TypeScript installed? TypeScript can be installed and managed via npm, the Node.js package manager. npm install -g typescript
6.How Do You Compile TypeScript Files? The extension for any TypeScript file is “.ts”. And any JavaScript file is TypeScript file as it is a superset of JavaScript. So change extension of “.js” to “.ts” file and your TypeScript file is ready.
To compile any .ts file into .js, use the following command. tsc <TypeScript File Name> For example, to compile “Helloworld.ts”: tsc helloworld.ts And the result would be helloworld.js.
7.Does TypeScript Support All Object Oriented Principles? Yes, It supports 4 main principles to Object Oriented Programming: -Encapsulation, -Inheritance, -Abstraction and -Polymorphism. 8.How do you Debug Any TypeScript File? To debug it, you need .js source map file. So compile the .ts file with the --sourcemap flag to generate a source map file. tsc -sourcemap file1.ts 9.How to Generate TypeScript Definition File from Any .ts File? We can generate TypeScript definition file from any .ts file via tsc compiler. Generating a TypeScript definition will make your TypeScript file reusable. tsc --declaration file1.ts
10.What is tsconfig.json File? The tsconfig.json file specifies the root files and the compiler options required to compile the project. { "compilerOptions": { "removeComments": true, "sourceMap": true }, "files": [ "main.ts", "othermodule.ts" ] } Within files section, define all the .ts files in the project. And when you invoke tsc without any other arguments with the above file in the current directory, it will compile all the files with the given compiler option settings.



Comments