-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson-06-Any-and-Unknown.js
More file actions
73 lines (73 loc) · 2.26 KB
/
Lesson-06-Any-and-Unknown.js
File metadata and controls
73 lines (73 loc) · 2.26 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
/*
Now lets now talk about any and Unknown datatype
in the previous lesson i showed you how we can use unknown
like this
*/
var logger1 = function (something) {
console.log(something);
};
// logger1('with unknown data type');
// logger1({datatype:'unknown data type'});
var logger2 = function (something) {
console.log(something);
};
// logger2('with any data type');
// logger2({datatype:'any data type'})
/*
Now in the upper example if we compare both functions everything is working fine
lets just take it to one step further
*/
/*
Now if you look at the below code you will notice that i tried to convert the data to uppercase
it will work with the string but as soon as we pass the object it will throw an error
because we the type became different
instead of using any if we use Unknown
we will get a typescript error on our ide . It means Unknown allows us to take anything but doesnot allow us to access anything
unless we do further checking
*/
var logger2_2 = function (something) {
console.log(something.toUpperCase());
};
// logger2_2('with unknown data type');
// logger2_2({datatype:'unknown data type'});
// const logger1_1 = (something : unknown) :void => {
// console.log(something.toUpperCase())
// }
// logger1_1('with unknown data type');
// logger1_1({datatype:'unknown data type'});
/*
so we what we need to do it we need to narrow it down for us
like this
This way we can prevent error
so what unknown is doing is preventing us from error and we all we need to do
is put more type safety while using unknown to prevent ourselves from errors
*/
var logger1_1_1 = function (something) {
if (typeof something === "string") {
console.log(something.toUpperCase());
}
else if (typeof something === "object") {
console.log(something);
}
};
var anyFoo = {
prop1: 'hello world',
prop2: { foo: 'bar' }
};
var unknownfoo = {
prop1: 'hello world',
prop2: { foo: 'bar' }
};
var unknownfoo_1 = {
prop1: 'hello worldddddd',
prop2: { foo: 'bar' }
};
if (typeof unknownfoo_1.prop1 === 'string') {
console.log(unknownfoo_1.prop1);
}
// else if(typeof unknownfoo_1 ==='object'){
console.log(unknownfoo_1.prop2.foo);
// }
/*
that's how we use type casting to access the data in the object of if we specify the type unknown
*/