Skip to content

Commit 5d4df4c

Browse files
committed
ts crash course
1 parent a2f6da7 commit 5d4df4c

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

11_CrashCourse/index.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var fullname = 'Banglore';
2+
fullname = 'Del';
3+
var num;
4+
num = 23;
5+
var names = [];
6+
names.push('s');
7+
function sum(a, b) {
8+
return a + b;
9+
}
10+
// console.log(sum(7, 2));
11+
//Tuples
12+
var address;
13+
address = [111, 'bhopal', 46331];
14+
var person;
15+
person = { firstName: 'Jojo Mike' };
16+
var anotherPerson;
17+
anotherPerson = { firstName: 'Nilesh', age: 21 };
18+
function add(a, b) {
19+
// optional only last parameters
20+
return b ? a + b : a;
21+
}
22+
console.log(add('2', '5'));
23+
var p = {
24+
name: 'delta',
25+
id: 34,
26+
age: 34,
27+
};
28+
console.log(p);
29+
//Union
30+
var p1;
31+
p1 = { name: 'APJ Abdul Kalam', age: 80, id: 23 };
32+
p1 = { name: 'Lal Bahadur Shashtri', age: 70, id: 2 };
33+
console.log(p1);
34+
//InterSection
35+
var p2;
36+
p2 = { name: 'Sam Bahadur', id: 234, age: 96, rollNo: 1 };
37+
var c;
38+
c = 1;
39+
c = 'one';
40+
var n = []; // never array
41+
//n.push('hello'); // can't push it never type
42+
//classes
43+
var Car = /** @class */ (function () {
44+
function Car(brand) {
45+
this.brand = brand;
46+
}
47+
Car.prototype.getBrand = function () {
48+
console.log(this.brand);
49+
};
50+
return Car;
51+
}());
52+
var c1 = new Car('Maruti');
53+
c1.getBrand();
54+
//public
55+
var CarX = /** @class */ (function () {
56+
function CarX(brand) {
57+
this.brand = brand;
58+
}
59+
CarX.prototype.getBrand = function () {
60+
console.log(this.brand);
61+
};
62+
return CarX;
63+
}());
64+
var c2 = new Car('Lamborgini');
65+
c2.getBrand();
66+
console.log(c2.brand);
67+
//private
68+
var CarY = /** @class */ (function () {
69+
function CarY(brand) {
70+
this.brand = brand;
71+
}
72+
CarY.prototype.getBrand = function () {
73+
console.log(this.brand);
74+
};
75+
return CarY;
76+
}());
77+
var c3 = new CarY('Jaguar');
78+
c3.getBrand();
79+
// c3.brand
80+
//protected
81+
var CarZ = /** @class */ (function () {
82+
function CarZ(brand) {
83+
this.brand = brand;
84+
}
85+
CarZ.prototype.getBrand = function () {
86+
console.log(this.brand);
87+
};
88+
return CarZ;
89+
}());
90+
var c4 = new CarZ('Tata');
91+
c4.getBrand();
92+
// c3.brand

11_CrashCourse/index.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
let fullname: string = 'Banglore';
2+
fullname = 'Del';
3+
4+
let num: number;
5+
num = 23;
6+
7+
const names: string[] = [];
8+
names.push('s');
9+
10+
function sum(a: number, b: number): number {
11+
return a + b;
12+
}
13+
// console.log(sum(7, 2));
14+
15+
//Tuples
16+
let address: [number, string, number];
17+
address = [111, 'bhopal', 46331];
18+
19+
let person: { firstName: string };
20+
person = { firstName: 'Jojo Mike' };
21+
22+
let anotherPerson: { firstName: string; age?: number };
23+
anotherPerson = { firstName: 'Nilesh', age: 21 };
24+
25+
function add(a: string, b?: string) {
26+
// optional only last parameters
27+
return b ? a + b : a;
28+
}
29+
console.log(add('2', '5'));
30+
31+
//interface
32+
interface Person {
33+
name: string;
34+
id: number;
35+
age: number;
36+
}
37+
let p: Person = {
38+
name: 'delta',
39+
id: 34,
40+
age: 34,
41+
};
42+
console.log(p);
43+
44+
interface Student {
45+
name: string;
46+
rollNo: number;
47+
age: number;
48+
}
49+
50+
//Union
51+
let p1: Person | Student;
52+
p1 = { name: 'APJ Abdul Kalam', age: 80, id: 23 };
53+
p1 = { name: 'Lal Bahadur Shashtri', age: 70, id: 2 };
54+
console.log(p1);
55+
56+
//InterSection
57+
let p2: Person & Student;
58+
p2 = { name: 'Sam Bahadur', id: 234, age: 96, rollNo: 1 };
59+
60+
//Type alias
61+
62+
type Count = string | number;
63+
let c: Count;
64+
c = 1;
65+
c = 'one';
66+
// c = false; // not assignable
67+
68+
type X = string & number; // never
69+
const n: [] = []; // never array
70+
//n.push('hello'); // can't push it never type
71+
72+
//classes
73+
74+
class Car {
75+
brand;
76+
constructor(brand) {
77+
this.brand = brand;
78+
}
79+
getBrand() {
80+
console.log(this.brand);
81+
}
82+
}
83+
let c1 = new Car('Maruti');
84+
c1.getBrand();
85+
86+
//public
87+
class CarX {
88+
constructor(public brand) {}
89+
getBrand() {
90+
console.log(this.brand);
91+
}
92+
}
93+
let c2 = new Car('Lamborgini');
94+
c2.getBrand();
95+
console.log(c2.brand);
96+
97+
//private
98+
class CarY {
99+
constructor(private brand) {}
100+
getBrand() {
101+
console.log(this.brand);
102+
}
103+
}
104+
let c3 = new CarY('Jaguar');
105+
c3.getBrand();
106+
// c3.brand
107+
108+
109+
//protected
110+
class CarZ {
111+
constructor(protected brand) {}
112+
getBrand() {
113+
console.log(this.brand);
114+
}
115+
}
116+
let c4 = new CarZ('Tata');
117+
c4.getBrand();
118+
// c3.brand

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "typescript-learn",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"typescript": "^5.5.4"
13+
}
14+
}

0 commit comments

Comments
 (0)