File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Objects
2
+
3
+ const user = {
4
+ name : "Tony" ,
5
+ email : "ironman@avengers.com" ,
6
+ isActive : true
7
+ }
8
+
9
+ // Weird Behavior
10
+ // function createUser({name: string, isPaid: boolean}){}
11
+ // let newUser = {name: "Peter", isPaid: false, email: "t@t .com"}
12
+ // createUser(newUser);
13
+
14
+ // Better way
15
+ function createCourse ( ) :{ name : string , price : number } {
16
+ return { name : "Full Stack Web Development" , price : 599 }
17
+ }
18
+
19
+ // Type Aliases
20
+
21
+ type User = {
22
+ name : string ;
23
+ email : string ;
24
+ isActive : boolean
25
+ }
26
+
27
+ function createUser ( user : User ) { }
28
+
29
+ createUser ( { name : "" , email : "" , isActive : false } )
30
+
31
+ // READONLY (Scenario Based)
32
+ type UserDetails = {
33
+ readonly _id : string ; // wont be able to change the value of _id down the road
34
+ name : string ;
35
+ email : string ;
36
+ isActive : boolean ;
37
+ creditCardDetails ?: number // optional (?)
38
+ }
39
+
40
+ let newUser : UserDetails = {
41
+ _id : "12345" ,
42
+ name : "Peter" ,
43
+ email : "parker@avengers.com" ,
44
+ isActive : true ,
45
+ }
46
+
47
+ type cardNumber = {
48
+ cardNumberVal : string
49
+ }
50
+
51
+ type cardDate = {
52
+ cardDateValue : string
53
+ }
54
+
55
+ // Using existing types to define another type
56
+ type cardDetails = cardNumber & cardDate & {
57
+ cvv : number
58
+ }
59
+
60
+ newUser . email = "pete@avengers.com" ;
61
+ // newUser._id = "234"; // Cannot assign to '_id' because it is a read-only property.
62
+
63
+
64
+ export { }
You can’t perform that action at this time.
0 commit comments