You know Java? Want to learn TYPESCRIPT without knowing JavaScript ! There you can learn TS , If you know Java

How to run typeScript file

  • First save file
  • Then in terminal type [ tsc filename.ts ]
  • After that node will change file name to .js extension and it will do automatically.
  • Afterword you have to type in terminal [ node filename.js ]
  • and it will print all the stuff in that file.

How to Define Variables

  • let grade : number = 99.8;
  • let found :boolean = true;
  • let fristName = String = "Anup";

How to print in TypeScript

Console.log(”Anything you want to print”);

How to define array

let ***reviews***: number[] = [5, 5, 4, 3, 2, 1];

How to use foreach loop in typescript

for (let sports of sport) {
console.log(sports);
}

How to use if statement

letsport: String[] = ["kunal", "sahil ", "vinay", "pattu"];

for (let sports ofsport) {
  if (sports == "pattu"){
console.log(sports + ">> gandu hai");
  }else {
console.log(sports)
  }
}

How to add elements to array

You know in TS the array is always growable / dynamic

letsport: String[] = ["kunal", "sahil ", "vinay", "pattu"];

sport.push("heera")
sport.push("moti")
sport.push("me na mangu")

How to make Constructor in TS

firstName: String;
lastName: String;

constructor(firstName: String, lastName: String) {
  this.firstName = firstName;
  this.lastName = lastName;
}

Now how to use this constructor , We can directly pass the value and print.

let ***myCustomer*** = new Customers("Kunal" , "Gupta");

How to make shortcut of Constructor and it work same TS

In this code we’re initilaze the value in constructor and it also work i.e

we declaer all things in a constructor and it work same as above constructor example

constructor(private _firstName: String,
            private _lastName: String) {
}

How not to generate .js file if there is error in .ts file

  • First delete the previously made js file then,
  • add and condition i.e - -noEmitOnError that means if there is an error in ts file it will not generate js file when you run tsc fileName.

What is Accessor?

Basically they are same as getter and setter in java. This is how we write don’t forget to add _ before variablt its a naming converntion.

private _firstName: String;

get firstName(): String {
  return this._firstName;
}
set firstName(value: String) {
    this._firstName = value;
  }

If there is an error of [Accessors are only available when targeting ECMAScript 5 and higher.]

Do , only change file name else everything same.

tsc --target ES5 --noEmitOnError Customers.ts

or we can add a tscConfig.json file or it is aready there if not make it and add in under complierOption,

"target": "es5",
"noEmitOnError": true,

How to export one class and import in another class.

its simple , the class you want to export just put export tag before class.

export class Customers {

and the where you want to import class just type

import { Customers } from "./Customers";

./is a path where that export file is present.

that’s it (change class name as per your requirements)

TO BE CONTINUE

Did you find this article valuable?

Support Kunal's { Blog } by becoming a sponsor. Any amount is appreciated!