TypeScrpt basics
+========= What is TypeScrpt? ==========+
Typescript is not entirely new language, it's a super set of JavaScript. that means any valid JS code is also valid typescript code.
Additional feature : -
--> Strong Typing / static typing,
--> Object oriented features
( class, interfaces, constructors, access modified like public and private, fills, Properties, Generics and so on. )
--> Compile time errors
--> great tooling (i like Intelligence that we get in code editors)
-->
Typescript is a super set of JavaScript
Many Browser Transpile Our TS code, into JS.
++========= Use full Commands below ============++
```
npm install -g typescript ( Install typescript Globally )
tsc -v ( show version )
touch main.ts ( creating new file )
code main.ts ( open in VScode )
tsc main.ts ( Compailing into main.js )
node main.ts ( run main.js )
```
++========= Declaring Variables ============++
Declearing a variable
let a: number;
let b: boolen;
let c: string;
let d: any;
let e: number[] = [1,2,3];
let f: any[] = [1, true, 'a', false];
const ColorRed = 0;
const ColorGreen = 1;
const ColorBlue = 2;
enum Color {purple = 0, Ra = 1, Green=2, Blue=3};
let BackgroundColor = Color.red;
++========= Type Assertions ============++
let message;
message = "abc";
let endsWithC = (<string>message).endsWith('c');
let alternativeway = (message as string).endsWith('c');
++========= Arrow Function ============++
//in javascripct
let log = function(message){
console.log(message);
}
// in TypeScript
let dolog = (message) => console.log(message);
<!-- we will call this in c# Lamb expresstion -->
<!-- anther way to write if multiple lines-->
let dolog = message => console.log(message);
let dolog = () => console.log();
let doLog = (message) => {
console.doLog(message);
}
++========= Interface | Custom types ============++
interface Point{
x: number,
y: number
}
<!-- use all ways Caps at interface Point -->
let drawPoint = (point: Point) = > {
<!-- -->
}
drawPoint({
x:4,
y:5
})
++========= Classes ============++
class Point {
x: number;
y: number;
draw(){
}
getDitance(anther:point){
}
}
++========= Objects ============++
<!-- create object.ts -->
<!-- touch obejts.ts -->
class Point {
x: number;
y: number;
draw(){
console.log("X: "+ this.x + ", y: "+ this.y);
}
getDistance(anther:Point){
}
}
let point = new Point();
point.x = 1;
point.y = 2;
point.draw();
<!-- runing commands -->
tsc object.ts && node object.js
result: x: 1, y:2
++========= Constructors ============++
class Point{
x: number;
y: number;
constructor(x?: number, y?:number){
this.x = x;
this.y = y;
}
draw(){
console.log("X: "+ this.x + ", y: "+ this.y);
}
getDistance(){
}
}
let point = new Point();
point.draw();
++========= Access Modifiers ============++
---> Public
---> private
---> protected
class Point{
private x:number;
private y:number;
}
++========= Access Modifiers with constructors Parameters ============++
class Point {
constructor (private x?: number, private y?: number) {
}
draw(){
console.log('x:'+ this.x + 'y:'+ this.y);
}
}
let point = new Point(1,2);
point.draw();
++========= Properties ============++
class Point {
constructor(private x?: number, private y?: number){
}
console.log('x:'+this.x+ "y:"+this.y);
get x(){
return this.x;
}
set x(value){
if(value < 0 )
throw new Error("value cannot be less then 0");
this.x = value;
}
}
let point = new Point();
let x = point.x;
point.x = 10;
point.draw();
++========= Modules ============++
file1.ts
export class Point {
constructors(private x?: number, private y?:number){
}
draw(){
console.log('x:'+this.x+ "y:"+this.y);
}
}
file2.ts
import { point } from './point';
let point = new Point();
Comments
Post a Comment