Skip to content

Commit 9c71178

Browse files
committed
revise
1 parent 875712a commit 9c71178

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

TypeScriptpracticeWithJavaScript.ts

+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
//javaScript variable
2+
3+
let name = "fahim";
4+
5+
let isName = true;
6+
7+
let data = [];
8+
9+
//TypeScript variable
10+
let nameT: string = "fahim";
11+
12+
let age: number = 25;
13+
14+
let isNameT: boolean = true;
15+
16+
let dataT: string[] = [];
17+
18+
//union types javaScript
19+
let mixed = [];
20+
mixed.push("fahim");
21+
mixed.push(25);
22+
mixed.push(true);
23+
24+
let id = " fahim";
25+
26+
//union types TypeScript
27+
let mixedT: any[] = [];
28+
mixedT.push("fahim");
29+
mixedT.push(25);
30+
mixedT.push(true);
31+
32+
let idT: string | number = "fahim";
33+
34+
//objects in javaScript
35+
36+
const person = {
37+
name: "fahim",
38+
age: 25,
39+
isName: false,
40+
};
41+
42+
console.log(person.name);
43+
44+
//object in typeScript
45+
interface personType {
46+
name: string;
47+
age: number;
48+
isName: boolean;
49+
}
50+
51+
const personT: personType = {
52+
name: "fahim",
53+
age: 25,
54+
isName: false,
55+
};
56+
57+
//function in javaScript
58+
59+
function hi() {
60+
console.log("hi");
61+
}
62+
63+
//function in typeScript
64+
65+
function hiT(): void {
66+
console.log("hi");
67+
}
68+
69+
function hiT2(name: string): number {
70+
console.log("hi", name);
71+
return 1;
72+
}
73+
74+
function hiT3(age: number): number {
75+
return age;
76+
}
77+
78+
function hiT4(age: number, name: string): string | number {
79+
return age + name;
80+
}
81+
82+
function hiT5(age: number, name: string): void {
83+
console.log(age, name);
84+
}
85+
86+
//Optional and Default Parameters in javaScript
87+
function greet(name, greeting) {
88+
return `${greeting}, {name}`;
89+
}
90+
91+
//Optional and Default Parameters in TypeScript
92+
function greetT(name: string, greeting: string): any {
93+
return `${greeting}, {name}`;
94+
}
95+
96+
function greetT1(name: string, greeting: string): string {
97+
return `${greeting}, {name}`;
98+
}
99+
100+
function greetT2(name: string, greeting: string = "hi"): string {
101+
return `${greeting}, {name}`;
102+
}
103+
104+
function greetT3(name: string, greeting?: string): void {
105+
console.log(`${greeting}, {name}`);
106+
}
107+
108+
//classes in javaScript
109+
110+
class Person {
111+
constructor(name, age) {
112+
this.name = name;
113+
this.age = age;
114+
}
115+
116+
speak() {
117+
console.log("hello I'm" + this.name + "and I am" + this.age + "years old");
118+
}
119+
}
120+
121+
const person1 = new Person("fahim", 25);
122+
console.log(person1.speak());
123+
124+
//classes in TypeScript
125+
126+
class PersonT {
127+
constructor(name: string, age: number) {
128+
this.name = name;
129+
this.age = age;
130+
}
131+
isName: boolean;
132+
133+
speak() {
134+
console.log(
135+
"hello I'm" +
136+
this.name +
137+
"and I am" +
138+
this.age +
139+
"years old" +
140+
this.isName
141+
);
142+
}
143+
}
144+
145+
const person1T = new PersonT("fahim", 25, true);
146+
console.log(person1T.speak());
147+
148+
//Enum in javaScript
149+
150+
const directions = {
151+
Up: "UP",
152+
Down: "DOWN",
153+
Left: "LEFT",
154+
Right: "RIGHT",
155+
};
156+
157+
//Enum in TypeScript
158+
const directionsT = {
159+
up: "up",
160+
down: "down",
161+
left: "left",
162+
right: "right",
163+
};
164+
165+
let move: string = directionsT.up;
166+
167+
//generics in javaScript
168+
function identity(arg) {
169+
return arg;
170+
}
171+
172+
//generics in TypeScript
173+
function identityT<T>(arg: T): T {
174+
return arg;
175+
}
176+
identityT<number>(25);
177+
identityT<string>("fahim");
178+
179+
//any and unknown in JavaScript
180+
let data = "fahim";
181+
182+
data = 1000; //no error
183+
184+
//any and unknown in TypeScript
185+
let dataT: unknown = "fahim";
186+
187+
dataT = 1000; //error
188+
189+
let dataT: any = "fahim";
190+
191+
dataT = 1000; //no error
192+
193+
// Difference Between unknown and any:
194+
// any: It allows you to assign any value without any type checking.
195+
// This can lead to errors because TypeScript won't warn you if
196+
// you're doing something that might be unsafe.
197+
198+
// unknown: It also allows any value, but you cannot do anything with it
199+
// until you check its type. TypeScript forces you
200+
// to perform some kind of checking before using it, ensuring safer code.
201+
202+
// Example of unknown:
203+
let value: unknown = 10; // Can be any type: number, string, object, etc.
204+
205+
// This will give an error because you cannot access properties directly on an `unknown` type.
206+
console.log(value.toString()); // Error: Object is of type 'unknown'.
207+
208+
// Before you use the value, you need to check its type.
209+
if (typeof value === "number") {
210+
console.log(value.toString()); // Safe: we know it's a number now.
211+
}
212+
213+
//type Assertion (casting) in javaScript
214+
let input = document.getElementById("input");
215+
input.value = "Hello World"; //not type safe
216+
217+
//type Assertion (casting) in TypeScript
218+
let input = document.getElementById("input") as HTMLInputElement;
219+
input.value = "Hello World"; //type safe

0 commit comments

Comments
 (0)