xxxxxxxxxx
// Declare a variable and set its initial value
let myVariable: string = "Hello, world!";
console.log(myVariable); // Output: Hello, world!
xxxxxxxxxx
class ABC {
private Name: string;
get name() : string {
return this.Name;
}
set name(setName: string){
this.Name = setName;
}
}
let abc = new ABC();
abc.name = "Albert Thomas"; //set property
console.log('My name is ' + abc.name); //get property
xxxxxxxxxx
// Creating a set
const mySet: Set<number> = new Set();
// Adding values to the set
mySet.add(1);
mySet.add(2);
mySet.add(3);
console.log(mySet); // Output: Set { 1, 2, 3 }
// Checking if a value exists in the set
console.log(mySet.has(2)); // Output: true
// Removing a value from the set
mySet.delete(3);
console.log(mySet); // Output: Set { 1, 2 }
// Iterating over the set
for (const item of mySet) {
console.log(item);
}
// Output:
// 1
// 2
xxxxxxxxxx
//Create a Set
let diceEntries = new Set();
//Add values
diceEntries.add(1);
diceEntries.add(2);
diceEntries.add(3);
diceEntries.add(4).add(5).add(6); //Chaining of add() method is allowed
//Check value is present or not
diceEntries.has(1); //true
diceEntries.has(10); //false
//Size of Set
diceEntries.size; //6
//Delete a value from set
diceEntries.delete(6); // true
//Clear whole Set
diceEntries.clear(); //Clear all entries
xxxxxxxxxx
- To setup simple typescript project follow these step
mkdir ts-proj
cd ts-proj
npm i typescript --save-dev
npx tsc --init
touch index.ts
npx tsc index.ts && node index.js