4 years ago
Afaq Arif
3,221 Views
4
The syntax is a set of rules to write programs. Every language specification defines its own syntax. A TypeScript program consists of the following.
Modules Functions Variables Statements and Expressions Comments
Your First TypeScript Code
Let us start with the traditional “Hello World” example.
var message:string = "Hello World"
console.log(message)
It will run follow on if JavaScript code on compiling.
//Generated by typescript 1.8.10
var message = "Hello World";
console.log(message);
Line 1 declares a variable by the name message. Variables are a mechanism to store values in a program. Line 2 prints the variable’s value to the prompt. Here, the console refers to the terminal window. The function log () is used to display text on the screen
Compile and Execute a TypeScript Program
We will see how to compile and execute a TypeScript program using Visual Studio Code. You need to follow the steps given below.
Step 1 − Save the file with the .ts extension. We shall save the file as Test.ts. If there is an error while saving, the code editor will mark it.
Step 2 − Right-click the TypeScript file under the Working Files option in VS Code’s Explore Pane. Select Open in Command Prompt option.
Step 3 − Use the following command on the terminal window To compile the file.
Step 4 − The file is compiled to Test.js. Now type the following in the terminal to run the program written.
Compiler Flags
Compiler flags allow you to change the behavior of the compiler during compilation. Each compiler flag has a setting that permits you to change how the compiler acts.
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 manual2. –module Load external modules3. –target Set the target ECMA version4. –declaration Generates an additional .d.ts file5. –removeComments Removes all comments from the output file6. –out Compile multiple files into a single output file7. –sourcemap Generate a sourcemap (.map) files8. –module noImplicitAny Disallows the compiler from inferring the any type9. –watch Watch for file changes and recompile them on the fly
Note − You can compile multiple files at once.
tsc file1.ts, file2.ts, file3.ts
Identifiers in TypeScript
Identifiers are names given to components in a program like variables, functions, etc. The rules for identifiers are.
Identifiers can have both, characters and digits. However, the identifier cannot start with a digit. Identifiers cannot have special symbols except for underscore (_) or a dollar sign ($). Identifiers cannot be used as keywords. Identifiers must be unique. Identifiers are case-sensitive. Identifiers cannot contain spaces.
The following tables lists a few examples of valid and invalid identifiers.
Valid identifiers Invalid identifiers firstName Var first_name first name num1 first-name $result 1number
TypeScript ─ Keywords
Keywords have a special meaning in the language framework. The following table lists some keywords in TypeScript.
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
Whitespace and Line Breaks
TypeScript neglects spaces, tabs, and newlines appearing in programs. You can use spaces, tabs, and newlines freely in your program. You can also format and set your programs freely in a neat and steady way. It makes the code simple to read and understand.
TypeScript is Case-sensitive
TypeScript is case-sensitive differentiating between uppercase and lowercase characters.
Semicolons are optional
Each line of instruction is called a statement . Semicolons are optional in TypeScript.
Example
console.log("hello world")
console.log("We are learning TypeScript")
TypeScript supports the following types of comments.
Single-line comments ( // ) − Any text between a // and the end of a line is treated as a commentMulti-line comments (/* */) − These comments may span multiple lines.
Example
//this is single line comment
/* This is a
Multi-line comment
*/
TypeScript and Object Orientation
TypeScript is Object-Oriented JavaScript . Object Orientation is a software development standard observing real-world modeling. It examines a program as an object collection that communicates with each other through a procedure called methods. TypeScript also supports object-oriented components.
Object − An object is a real-time presentation of any system. According to Grady Brooch, every object must have three characteristics −State − It describes by the features of an object. Behavior − It describes how the object will behave. Identity − It is a unique value that picks out an object from a set of similar such objects. Class − A class is a blueprint for creating objects in OOP. A class sums up data for the object.Method − Methods help to communicate between objects.
Example: TypeScript and Object Orientation
class Greeting {
greet():void {
console.log("Hello World!!!")
}
}
var obj = new Greeting();
obj.greet();
The above example defines a class Greeting . The class has a method greet () . The method prints the string “Hello World” on the terminal. The new keyword creates an object of the class (obj). The object calls for the method greet () .
It will generate following JavaScript code on composing.
//Generated by typescript 1.8.10
var Greeting = (function () {
function Greeting() {
}
Greeting.prototype.greet = function () {
console.log("Hello World!!!");
};
return Greeting;
}());
var obj = new Greeting();
obj.greet()
The output of the above program is given below.
4 years ago
Afaq Arif
3,221 Views
4