Skip to content

Commit 527b3cb

Browse files
committed
union types
1 parent 02e9061 commit 527b3cb

File tree

6 files changed

+189
-8
lines changed

6 files changed

+189
-8
lines changed

03_Objects/object.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@ var myObject = {
66
77
isActive: true,
88
};
9-
function createUserr(_a) {
10-
var any = _a.namee;
11-
return { namee: namee };
12-
}
13-
createUserr('delta');
9+
// function createUserr({ namee: any }) {
10+
// return { namee };
11+
// }
12+
// createUserr('delta');
1413
function createCourse() {
1514
return { name: 'react', price: 7363 };
1615
}
1716
console.log(createCourse());
17+
function createPerson(user) {
18+
return { name: 'me', email: 'me@', isActiver: true };
19+
}
20+
createPerson({ name: 'me', email: 'me@', isActiver: true });
21+
var myUser = {
22+
_id: 'fd3j2390',
23+
name: 'jojo',
24+
25+
isActive: true,
26+
};
27+
// myUser._id = '38493'; // it not mutable bcs of readonly
28+
console.log(myUser._id); /// its consoling same which already decalare in first place

03_Objects/object.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,43 @@ type User = {
2222
email: string;
2323
isActiver: boolean;
2424
};
25-
function createPerson(user: User):User {
26-
return { name: 'me', email: 'me@', isActiver: true };
25+
function createPerson(user: User): User {
26+
return { name: 'me', email: 'me@', isActiver: true };
2727
}
2828
createPerson({ name: 'me', email: 'me@', isActiver: true });
2929

30+
//Example 2 -- ? -- Readonly
3031

31-
//Example 2
32+
type Admin = {
33+
//without readonly its mutable
34+
readonly _id: string;
35+
name: string;
36+
email: string;
37+
isActive: boolean;
38+
creditcardDetails?: number; // ? optional ,no error
39+
};
40+
41+
let myUser: Admin = {
42+
_id: 'fd3j2390',
43+
name: 'jojo',
44+
45+
isActive: true,
46+
};
3247

48+
// myUser._id = '38493'; // it not mutable bcs of readonly
49+
console.log(myUser._id); /// its consoling same which already decalare in first place
3350

51+
// MIXING TYPES
3452

53+
type cardNumber = {
54+
cardnumber: number;
55+
};
56+
type cardDate = {
57+
cardDate: number;
58+
};
59+
type CardDetails = cardNumber &
60+
cardDate & {
61+
cardCvv: number;
62+
};
3563

3664
export {};

04_Arrays/array.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
// Example 1 of Writing
4+
var superHeros = [];
5+
superHeros.push('399');
6+
console.log(superHeros);
7+
// Example 2 of Writing
8+
var heroPower = [];
9+
heroPower.push(293);
10+
console.log(heroPower);
11+
var allUser = [
12+
{ name: "jeet", isActivated: true },
13+
{ name: "delta", isActivated: false }
14+
];
15+
console.log(allUser);
16+
//Example 4 Array of Array
17+
var coordiantes = [
18+
[2394, 394, 394],
19+
[23, 33, 4, 4]
20+
];
21+
console.log(coordiantes);

04_Arrays/array.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Example 1 of Writing
2+
const superHeros: string[] = [];
3+
superHeros.push('399');
4+
console.log(superHeros);
5+
6+
// Example 2 of Writing
7+
const heroPower: Array<number> = [];
8+
heroPower.push(293)
9+
console.log(heroPower);
10+
11+
// Example 3 with type
12+
type User = {
13+
name: string
14+
isActivated: boolean
15+
}
16+
const allUser: User[]=[
17+
{name :"jeet", isActivated:true},
18+
{name :"delta", isActivated:false}
19+
]
20+
console.log(allUser);
21+
22+
//Example 4 Array of Array
23+
const coordiantes: number[][]= [
24+
[2394,394,394],
25+
[23,33,4,4]
26+
]
27+
console.log(coordiantes);
28+
29+
30+
31+
32+
33+
34+
export {};

05_Union/union.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
// Example of union type where can you two type type when not sure
3+
Object.defineProperty(exports, "__esModule", { value: true });
4+
var score = 45;
5+
score = '34'; // allow ok
6+
score = 12394; // allow ok
7+
var userCreate = { name: 'lieutenant', id: '390' }; //ok
8+
userCreate = { username: 'Brigadier', id: 3903 }; //ok
9+
// Union Type with Fn
10+
function gebDatabseId(id) {
11+
console.log("Db id is ".concat(id));
12+
}
13+
gebDatabseId(34); // ok
14+
gebDatabseId('232093dfdfckj'); // ok
15+
// Probleam comes inside of Fn like this and solve it by if else
16+
function getId(id) {
17+
// id.toUpperCase()// throwing error
18+
if (typeof id === 'string') {
19+
id.toUpperCase();
20+
console.log("Account Id is ".concat(id.toUpperCase()));
21+
}
22+
console.log("Account Id is ".concat(id));
23+
}
24+
getId(234);
25+
getId('dfjh3489');
26+
/// Union type with Array
27+
var Arr1 = [2, 3, 3];
28+
Arr1.push(234);
29+
var Arr2 = ['Alpha', 'Bravo', 'Charlie'];
30+
Arr2.push('Zulu');
31+
var Arr3 = [122, 344, 'Alpha', 'Bravo'];
32+
console.log(Arr1);
33+
console.log(Arr2);
34+
console.log(Arr3);

05_Union/union.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Example of union type where can you two type type when not sure
2+
3+
let score: number | string = 45;
4+
score = '34'; // allow ok
5+
score = 12394; // allow ok
6+
7+
// Union Type with type
8+
type User = {
9+
name: string;
10+
id: string;
11+
};
12+
type Admin = {
13+
username: string;
14+
id: number;
15+
};
16+
let userCreate: User | Admin = { name: 'lieutenant', id: '390' }; //ok
17+
userCreate = { username: 'Brigadier', id: 3903 }; //ok
18+
19+
// Union Type with Fn
20+
function gebDatabseId(id: number | string) {
21+
console.log(`Db id is ${id}`);
22+
}
23+
gebDatabseId(34); // ok
24+
gebDatabseId('232093dfdfckj'); // ok
25+
26+
// Probleam comes inside of Fn like this and solve it by if else
27+
function getId(id: number | string) {
28+
// id.toUpperCase()// throwing error
29+
if (typeof id === 'string') {
30+
id.toUpperCase();
31+
console.log(`Account Id is ${id.toUpperCase()}`);
32+
}
33+
console.log(`Account Id is ${id}`);
34+
}
35+
getId(234);
36+
getId('dfjh3489');
37+
38+
/// Union type with Array
39+
const Arr1: number[] = [2, 3, 3];
40+
Arr1.push(234);
41+
const Arr2: string[] = ['Alpha', 'Bravo', 'Charlie'];
42+
Arr2.push('Zulu');
43+
const Arr3: (number | string)[] = [122, 344, 'Alpha', 'Bravo'];
44+
console.log(Arr1);
45+
console.log(Arr2);
46+
console.log(Arr3);
47+
48+
// Specific / Default value want to set like
49+
let seatAllotment: 'Garib' | 'Middle' | 'Amir';
50+
// seatAllotment= "crew" // error
51+
seatAllotment = 'Middle';
52+
53+
export {};

0 commit comments

Comments
 (0)