-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson-05-Void-and-Null.ts
More file actions
137 lines (95 loc) · 2.94 KB
/
Lesson-05-Void-and-Null.ts
File metadata and controls
137 lines (95 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
In this chapter we will cover the types of void null and undefined
so the first one is void
The void type is which doesn't return anything
lets have a look
*/
// const logger = (something ) :void => {
// console.log(something)
// }
/*
if you have uncomment the above line of code you will se we are facing an error
i.e Parameter 'something' implicitly has any type
we can get rid of this error with just giving the parameter a type of any or unknown
dont worry ill cover any and unknown later in the next chapter
but for now just go along the ride with me ^^
*/
/*
now i just added the the type unknown and now
lets just return the Logger function and see what will happen
*/
const Logger = (something:unknown ) :void => {
console.log(something)
}
// const a = Logger('i am logging a string');
// console.log({a});
/*
so now if you run this function and see the console you will see
the functions does run and we saw our console which will show this result
i am logging a string
{a: undefined}
but it didnt return anything
the proof is we stored our function in a variable a and we can see it shows undefined
if the function return the value it should get store in that variable
so that is void type
*/
/*
Now lets talk about NUll type
lets just first create an interface of user
*/
interface Userr{
id : number;
email :string;
full_name : string|null;
full_age : string|null;
}
/*
so i assigned null type to name and age
what i am trying to say to property name and age is
A User could have or have not name and age
just like laravel where we use nullable
it is similar to that
*/
/*
Lets take a real life example as we have created an interface of user
now suppose we want to create an account of user after accepting the email
we can do it like this
Now the example i created a function where we will recieve data
as we know we will hit some api and in return we will get this data from there in the form of an object
right now i just did some hard coding to show how the result will be displayed
*/
const CreateUser = (email :string) :Userr => ({
id: 1,
email,
full_name: null,
full_age: null
})
// const user = CreateUser('haris@nodesol.com');
// console.log({user});
/*
what if in the above CreateUser function we dont want any property for example
we dont want the age property what we can do is we can remove it from the function and
in our interface we will use ? before the :
let me show you how
here is an interface
*/
interface Userrr{
id : number;
email :string;
full_name : string|null;
full_age ?: string|null;
}
/*
And here we just removed it from the function and it will not return any error
as the question mark is stating that data can be undefined or null type
*/
const CreateUserr = (email :string) :Userrr => ({
id: 1,
email,
full_name: null,
})
const userr = CreateUserr('haris@nodesol.com');
console.log({userr});
/*
This is all for void and null type
*/