TypeScript is a strongly typed, object oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft.
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is pure object oriented with classes, interfaces and statically typed like C# or Java. The popular JavaScript framework Angular 2.0 is written in TypeScript.
TypeScript is JavaScript for application-scale development.
TypeScript is JavaScript plus some additional features.
Typescript is an Open Source technology. It can run on any browser, any host, and any OS. You will need the following tools to write and test a Typescript program -
You can write your TypeScript source code in any text editor like Notepad, Notepad++, vi, vim etc.Editors used may vary with Operating Systems.
The source files are typically named with the extension .ts
The TypeScript compiler (tsc) converts the instructions written in TypeScript to its JavaScript equivalent. It itself a .ts file compiled down to JavaScript (.js) file.The TSC (TypeScript Compiler) is a source-to-source compiler (transcompiler / transpiler).
The TSC produces an equivalent JavaScript source code from the Typescript file given as an input to it. This process is termed as transpilation. However, the compiler rejects any raw JavaScript file passed to it. The compiler deals with only .ts or .d.ts files.
Node.js is an open source, cross-platform runtime environment for server-side JavaScript. Node.js is required to run JavaScript without a browser support. It uses Google V8 JavaScript engine to execute code. Node is available here :- node.js
To verify if the installation was successful, enter the command node -v in the terminal window.
Type the following command in the terminal window to install TypeScript.
npm install -g typescript
Typescript can be built on a plethora of development environments like Visual Studio, Sublime Text 2, WebStorm/PHPStorm, Eclipse, Brackets, etc.
var msg:string = "Welcome to Typescript Learning" console.log(msg)
tsc test.ts
The corresponding .js(test.js) file will be created after compilation. tsc --declaration test.ts
node test.js
Multiple files can be compiled at once. tsc file1.ts, file2.ts, file3.ts
The following table lists some common flags associated with the TSC compiler. A typical command-line usage uses some or all switches.
S.No. | Compiler flag & Description |
---|---|
1. | --help Displays the help manual |
2. | --module Load external modules |
3. | --target Set the target ECMA version |
4. | --declaration Generates an additional .d.ts file |
5. | --removeComments Removes all comments from the output file |
6. | --out Compile multiple files into a single output file |
7. | --sourcemap Generate a sourcemap (.map) files |
8. | --module noImplicitAny Disallows the compiler from inferring the any type |
9. | --watch Watch for file changes and recompile them on the fly |
Identifiers are names given to elements in a program like variables, functions etc. The rules for identifiers are -
break | as | any | switch |
case | if | throw | else |
var | number | string | get |
module | type | instanceof | typeof |
public | private | enum | export |
finally | for | while | void |
null | super | this | new |
in | return | true | false |
any | extends | static | let |
package | implements | interface | function |
new | try | yield | const |
continue | do | catch |
TypeScript ignores spaces, tabs, and newlines that appear in programs. You can use spaces, tabs, and newlines freely in your program and you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.
TypeScript is case-sensitive. This means that TypeScript differentiates between uppercase and lowercase characters.
Each line of instruction is called a statement. Semicolons are optional in TypeScript.A single line can contain multiple statements. However, these statements must be separated by a semicolon.
TypeScript supports these object oriented components
class welcome { print():void { console.log("Welcome to Typescript Learning world!!!") } } var obj = new welcome(); obj.print()
The above example defines a class welcome. The class has a method print (). The method prints the string "Welcome to Typescript Learning world!!!" on the terminal. The new keyword creates an object of the class (obj). The object invokes the method print ().
On compiling, it will generate following JavaScript code.
var welcome = /** @class */ (function () { function welcome() { } welcome.prototype.print = function () { console.log("Welcome to Typescript Learning world!!!"); }; return welcome; }()); var obj = new welcome(); obj.print();
Welcome to Typescript Learning world!!!
See also : TypeScript Programmning Language
If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.