TypeScript tutorial provides basic and advanced concepts of TypeScript. Our TypeScript Tutorial is designed for beginners and professionals both.
TypeScript is an open-source object-oriented programing language. It is developed and maintained by Microsoft under Apache 2 license. It was developed by Anders Hejlsberg, who is also one of the core members of the development team of C# language. TypeScript is a strongly typed superset of JavaScript that compiles to plain JavaScript. TypeScript is a language for application-scale JavaScript development. 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.
Q1. What are the benefits of TypeScript?
Ans. TypeScript has following benefits.
- It helps in code structuring.
- Use class based object oriented programming.
- Impose coding guidelines.
- Offers type checking.
- Compile time error checking.
- Intellisense.
Q2. What are different components of TypeScript?
Ans. There are mainly 3 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.
Q3. How can you get TypeScript and install it?
Ans. TypeScript can be installed and managed via npm, the Node.js package manager. To install TypeScript, first ensure the npm is installed properly. And then run following command to install TypeScript globally on your system.
1
| npm install -g typescript |
TypeScript is included in Visual Studio 2013 Update 2 and Visual Studio 2015 by default.
Q4. How do you compile TypeScript files?
Ans. 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 following command.
1
| tsc <TypeScript File Name> |
For example, to compile “Helloworld.ts”
1
| tsc helloworld.ts |
And the result would be helloworld.js
Q5. Is it possible to combine multiple .ts files into a single .js file?
Ans. Yes, it possible. While compiling add
--outFILE [OutputJSFileName]
option.
1
| tsc --outFile comman.js file1.ts file2.ts file3.ts |
This will compile all 3 “.ts” file and output into single “comman.js” file. And what will happen if you don’t provide a output file name.
1
| tsc --outFile file1.ts file2.ts file3.ts |
In this case, file2.ts and file3.ts will be compiled and the output will be placed in file1.ts. So now your file1.ts contains JavaScript code.
Q6. Is it possible to compile .ts automatically with real time changes in .ts file?
Ans. Yes. Using
--watch
compiler option this can be achieved.
1
| tsc --watch file1.ts |
This will first compile file1.ts in file.js and watch for the file changes. As soon as there is any change detected, it will compile it again. But you need to ensure that command prompt must not be closed, used with
--watch
option.
Q7. Does TypeScript support all object oriented principles?
Ans. The answer is YES. There are 4 main principles to Object Oriented Programming: Encapsulation, Inheritance, Abstraction, and Polymorphism. TypeScript can implement all four of them with its smaller and cleaner syntax.
Q8. Which object oriented terms are supported by TypeScript?
Ans. TypeScript supports following object oriented terms.
- Modules
- Classes
- Interfaces
- Data Types
- Member functions
Q9. Which are the different data types supported by TypeScript?
Ans. TypeScript supports following data types.
- Boolean
var bValue: boolean = false;
- Number
var age: number = 16;
- String
var name: string = "jon";
- Array
var list:number[] = [1, 2, 3];
- Enum12
enum Color {Red, Green, Blue};
var
c: Color = Color.Green;
- Any
var unknownType: any = 4;
- Void12
function
NoReturnType(): void {
}
Q10. How TypeScript is optionally statically typed language?
Ans. TypeScript is referred as optionally statically typed, which means you can ask the compiler to ignore the type of a variable. Using
any
data type, we can assign any type of value to the variable. TypeScript will not give any error checking during compilation.
1
2
3
| var unknownType: any = 4; unknownType = "Okay, I am a string" ; unknownType = false; // A boolean. |
Q11. What are modules in TypeScript?
Ans. Modules are the way to organize code in TypeScript. Modules don’t have any features, but can contain classes and interfaces. It is same like namespace in C#.
Q12. What are classes in TypeScript?
Ans. The concept of classes is very similar to .Net/Java. A Class can have constructor, member variables, properties and methods. TypeScript also allows access modifiers “private” and “public” for member variables and functions.
0 comments:
Post a Comment