In this article ..we are learn basic concept in TypeScript.
Variable Declaration:
Variable: Variable is a named place in memory where some data/value can be stored. According to the word variable, it can be said that the value of a variable can be changed/vary.
While declaring a variable some rules have to be followed:
1. Variable name can contains alphabets both Upper-case as well as Lower-case and digits also.
2.Variable name cant start with digit.
3.We can use _ and $ special character only, apart from these other special characters are not allowed.
Variable declaration:
A variable must be declared before it is used.use the bar keyword to declare a variable.
We can declare a variable in multiple ways like below:
var Identifier:Data-type = value;
var Identifier: Data-type;
var Identifier = value;
var Identifier;
Examples:
1.var name:number = 10; Here name is a variable which can store only Integer type data.
2.var name:number; Here name is a variable which can store only Integer type data. But by
default its value set to undefined.
3.var name = 10; Here while declaring variable we are not specifying data-type. Therefore
compiler decide its data type by seeing its value i.e. number here.
4.var name; Here while declaring variable we are not specifying data-type as well as we are not
assigning any value also. then compiler takes its data type as any.its value is set to undefined by default.
Creating object:
- An object is an instance of class which contains set of key value pairs.
- It’s value may be scalar values or functions or even array of other objects.
Syntax of creating a object:
var object_name = new class_name();
Creating class:
- Classterm of OOPs is a blueprint for creating objects.
- In class group of objects which have common properties.
- Class contain fields, methods, constructors, Blocks, Nested class and interface.
- Use the class keyword to declare a class in TypeScript.
Syntax to declare a class:
class class_Name{
field;
method;
}
Import:
- Importing is just about as easy as exporting from a module.
- A statement to use primitive values, objects, functions, or classes that are exported from another module. Typically, an import is static, but it can be dynamic too
- Importing an exported declaration is done through using one of the import forms below:
- Import a single export from a module
import { ZipCodeValidator } from "./ZipCodeValidator";
let myValidator = new ZipCodeValidator();
Example:
Export:
- A statement to export primitive values, objects, functions, or classes from the module for the purpose of being used by other modules.
- Exporting a declaration Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword.StringValidator.tsexport interface StringValidator {isAcceptable(s: string): boolean;}
Example:
TypeScript Configuration:
- The tsconfig.json file indicates the root directory of TypeScript.
- The tsconfig.json file specifies the root files as well as the compiler options for compiling a program.
- As we run tsc command for each .ts file for converting it into .js file. The tsconfig.json file converts the all .ts file into .js and make our work easy.
- For tsconfig.json file run following command on terminal :
> tsc --init
- The above command automatically creates a tsconfig.json file.
- We can use a "outDir" for creating a new folder which having the .js files of our module, as i created in above image.
Thank you...
No comments:
Post a Comment