Type Script Programme Challenges

  • By
  • October 20, 2022
  • JavaScript
TypeScript

Type Script Programme Challenges

error TS2583: Cannot find name ‘Map’.  

s1.ts 

let m1 =new Map(); 

m1.set(“meena”,60); 

m1.set(“heena”,80); 

m1.set(“shruti”,70); 

//iterate over map 

for( let k1 of m1.entries()) 

 console.log(“Map key:- “+ k1); 

// keys 

for(let k of m1.keys()) 

 console.log(k); 

//values 

for(let k of m1.values()) 

 console.log(k); 

/////s1.js 

let m1 = new Map(); 

m1.set(“meena”, 60); 

m1.set(“heena”, 80); 

m1.set(“shruti”, 70);

//iterate over map 

for (let k1 of m1.entries()) { 

 console.log(“Map key:- ” + k1); 

// keys 

for (let k of m1.keys()) { 

 console.log(k); 

//values 

for (let k of m1.values()) { 

 console.log(k); 

error TS2583: Cannot find name ‘Map’. Try changing the ‘lib’ compiler option to ‘es2015’ or later. 

4 let m1=new Map(); 

Resolvtion: 

Use following command 

tsc –target es6 s2.ts; 

node s2.js 

“duplicate identifier” Typescript error message 

S1.ts 

/ what is dublicate identifire issue… 

//how to resolve  

export{} 

class student{ 

 method() 

 {

 console.log(“method describe here…”); 

 } 

var s1=new student(); 

//object creation 

s1.method(); 

duplicate identifier 

Why am I getting this and many more errors of this kind? 

S1.js 

“use strict”; 

// exports.__esModule = true; 

var student = /** @class */ (function () { 

 function student() { 

 } 

 student.prototype.method = function () { 

 console.log(“method describe here…”); 

 }; 

 return student; 

}()); 

var s1 = new student(); 

//object creation 

s1.method(); 

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

 Function in Typescript : 

Example1  

Fun1.ts 

// function nameoffunc() 

// {

// body 

// } 

//defination of fucntion 

function display() 

 console.log(“welcoem to learn Typescript”); 

// function call 

 display(); 

 display() 

//parametrised function function 

function Add(x,y) 

 console.log(“Add:-“+(x+y)); 

Add(3,2); 

Add(5,3); 

// option parameter 

 // function name(parameter1[:type],paraemter2[:type],parameter3?[:type]) function EmpDetail(id:number,name:string,e_mail_id?:string) { 

 console.log(“ID:”,id,”Name:”,name); 

EmpDetail(111,”mahesh”); 

// default parameter 

// function name(parameter1[:type],paraemter2[:type]=default_value){} function showName(name:String,greeting:string=”All the best”)

 // console.log(greeting +’ ‘+name); 

 return greeting +’ ‘+name; 

// showName(‘rahule’) 

// var ans=showName(‘somthing’); 

// console.log(ans); 

console.log(showName(‘meena’,’join us’)); 

Fun1.js 

// function nameoffunc() 

// { 

// body 

// } 

//defination of fucntion 

function display() { 

 console.log(“welcoem to learn Typescript”); 

// function call 

display(); 

display(); 

//parametrised function function 

function Add(x, y) { 

 console.log(“Add:-” + (x + y)); 

Add(3, 2); 

Add(5, 3); 

// option parameter 

// function name(parameter1[:type],paraemter2[:type],parameter3?[:type]) function EmpDetail(id, name, e_mail_id) {

 console.log(“ID:”, id, “Name:”, name); 

EmpDetail(111, “mahesh”); 

// default parameter 

// function name(parameter1[:type],paraemter2[:type]=default_value){} 

function showName(name, greeting) { 

 if (greeting === void 0) { greeting = “All the best”; } 

 // console.log(greeting +’ ‘+name); 

 return greeting + ” ” + name; 

// showName(‘rahule’) 

// var ans=showName(‘somthing’); 

// console.log(ans); 

console.log(showName(‘meena’, ‘join us’)); 

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Rest Parametrised Function 

// rest parameter is used to pass one or more values to a function. 

//declare prefix (…) before the parameter 

//where we have an undetermined number of parameter 

// rule -only one rest para is allowed in function 

// it must be an array 

// it must be the last parameter in parameter list 

// syntaxt: 

//function name(par1:[type],…para[:type]) 

Example: 

S1.ts 

function sum(a:number, …b:number[]){ 

 let res=a; //a=1 b[0]=1 

 console.log(a);

 for(var i=0 ; i<b.length ;i++) 

 { 

 // res=res+b[i]; //1+b[0] //1+1 //2 

 console.log(b[i]); 

 } 

 // console.log(res); 

//function call 

 sum(1,4,6,3,8); 

S1.js 

function sum(a) { 

 var b = []; 

 for (var _i = 1; _i < arguments.length; _i++) { 

 b[_i – 1] = arguments[_i]; 

 } 

 var res = a; //a=1 b[0]=1 

 console.log(a); 

 for (var i = 0; i < b.length; i++) { 

 // res=res+b[i]; //1+b[0] //1+1 //2 

 console.log(b[i]); 

 } 

 // console.log(res); 

//function call 

sum(1, 4, 6, 3); 

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Switch Case Using Type script 

Example: 

S1.ts 

// let ch:number=3;

let s:string=’B’; 

let a:number=2; 

let b:number=3; 

switch(s) 

 case ‘A’: 

 console.log(“case 1….Addition :-“+(a+b)); 

 break; 

 case ‘B’: 

 console.log(“case 2…Substarction :-“+(a-b));  break; 

 case ‘C’: 

 console.log(“case 3….Multiplication:-“+(a*b));  break; 

 default: 

 { 

 console.log(“no case mathced..”);  }  

 break; 

S1.js 

// let ch:number=3; 

var s = ‘B’; 

var a = 2; 

var b = 3; 

switch (s) { 

 case ‘A’: 

 console.log(“case 1….Addition :-” + (a + b));  break; 

 case ‘B’:

 console.log(“case 2…Substarction :-” + (a – b)); 

 break; 

 case ‘C’: 

 console.log(“case 3….Multiplication:-” + (a * b)); 

 break; 

 default: 

 { 

 console.log(“no case mathced..”); 

 } 

 break; 

 

 

Enum demo Example: 

S1.ts 

//Numeric 

enum Direction{ 

Up=1, 

Down=7, 

Left=9, 

Right=10, 

console.log(Direction); 

//Enum as a function argument 

 function checkDirection(dir:Direction):void 

 { 

 console.log(dir);

 } 

checkDirection(Direction.Right); //String 

 enum color{ 

 Red=’red’, 

 Pink=’pink’, 

 Blue=’blue’, 

 } 

 function chekcColor(col : color):void{ 

 console.log(col); 

 } 

 //function call 

 chekcColor(color.Red); //Hetrogenous 

 enum color1{ 

 Red=’red’, 

 Pink=2, 

 Blue=’blue’, 

 } 

 console.log(color1.Pink);

 console.log(color1.Blue); 

// default index start with zero 

S1.js 

//Numeric 

var Direction; 

(function (Direction) { 

 Direction[Direction[“Up”] = 1] = “Up”;  Direction[Direction[“Down”] = 7] = “Down”;  Direction[Direction[“Left”] = 9] = “Left”;  Direction[Direction[“Right”] = 10] = “Right”; })(Direction || (Direction = {})); 

console.log(Direction); 

//Enum as a function argument 

function checkDirection(dir) { 

 console.log(dir); 

checkDirection(Direction.Right); 

//String 

var color; 

(function (color) { 

 color[“Red”] = “red”; 

 color[“Pink”] = “pink”; 

 color[“Blue”] = “blue”; 

})(color || (color = {})); 

function chekcColor(col) { 

 console.log(col); 

//function call 

chekcColor(color.Red); 

//Hetrogenous

var color1; 

(function (color1) { 

 color1[“Red”] = “red”; 

 color1[color1[“Pink”] = 2] = “Pink”;  color1[“Blue”] = “blue”; 

})(color1 || (color1 = {})); 

console.log(color1.Pink); 

console.log(color1.Blue); 

// default index start with zero

 

Author:-

Pooja Ghodekar

Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*