1 00:00:00,940 --> 00:00:05,100 In the last video we installed a typed definition file for faker as a quick side. 2 00:00:05,100 --> 00:00:10,950 One of those benefits of type definition files is that we can now hover over faker right here and then 3 00:00:10,950 --> 00:00:15,510 if you're on Mac hold down the command key or if you're on Windows hold down control. 4 00:00:15,510 --> 00:00:19,210 You'll notice that faker turns into a clickable link of sorts. 5 00:00:19,260 --> 00:00:24,440 If we now click on that we'll be taken to the typedef definition file that we just installed. 6 00:00:24,450 --> 00:00:30,930 Notice how the name of this file is index dot D dot t s traditionally type definition files are named 7 00:00:30,930 --> 00:00:33,210 with extensions of D dot T.S.. 8 00:00:33,330 --> 00:00:37,710 So anytime you are looking at some typescript code and you see a file with that extension that means 9 00:00:37,710 --> 00:00:41,800 you are looking at a type definition file inside this file. 10 00:00:41,810 --> 00:00:44,610 You're going to find nothing but typescript syntax. 11 00:00:44,810 --> 00:00:50,570 So the only goal of this file right here is to describe the different types of values functions objects 12 00:00:50,570 --> 00:00:53,840 classes that exist inside the faker library. 13 00:00:53,990 --> 00:00:58,040 So there will not actually be any functions that are implemented inside of here. 14 00:00:58,040 --> 00:01:03,830 Instead you instead you will only see a definition of the different types that are available if you 15 00:01:03,830 --> 00:01:07,500 look inside of here we can actually use this as a source of documentation. 16 00:01:07,550 --> 00:01:12,410 So for example remember at the official documentation we looked at just a moment ago we saw that address 17 00:01:12,410 --> 00:01:13,310 module. 18 00:01:13,460 --> 00:01:19,520 So this right here is telling typescript that this library has a address object and inside of it are 19 00:01:19,520 --> 00:01:24,530 different functions like zip code city city prefix and so on. 20 00:01:24,530 --> 00:01:29,690 We can see the different arguments that each of them take and a different type of values that they return. 21 00:01:29,690 --> 00:01:35,210 So like I said we can use this file as kind of an alternate forms of documentation to understand how 22 00:01:35,210 --> 00:01:40,060 to work with a given library so in this case we can take a look at address. 23 00:01:40,150 --> 00:01:45,360 If we scroll down you'll notice the latitude and longitude are two functions inside there. 24 00:01:45,400 --> 00:01:50,580 We're gonna use those functions to actually randomly generate our latitude longitude as a quick side 25 00:01:50,580 --> 00:01:56,520 note I want you to notice how latitude longitude return Strings which is actually kind of not very typical 26 00:01:56,520 --> 00:02:00,660 usually we reflect latitude and longitude as numbers. 27 00:02:00,660 --> 00:02:05,250 So having them being returned here as a string means that might be a little bit unexpected and we might 28 00:02:05,250 --> 00:02:09,420 have to deal with that at some point in time but we'll just see what happens. 29 00:02:09,450 --> 00:02:14,160 All right I'm going to close this file and back inside of my user dot t s file. 30 00:02:14,210 --> 00:02:16,940 We're not going to do some initialization of our name. 31 00:02:17,060 --> 00:02:20,300 Latitude and longitude inside of our constructor. 32 00:02:20,300 --> 00:02:24,590 So inside the constructor as soon as we make an instance of a user we're going to randomly generate 33 00:02:24,650 --> 00:02:27,650 a name and assign it to that name property. 34 00:02:27,650 --> 00:02:33,400 So inside of here I'll say this dot name is going to come from faker. 35 00:02:33,590 --> 00:02:35,540 We're going to get the name module. 36 00:02:35,540 --> 00:02:40,220 Remember we saw that in the official documentation his name right here and we're going to generate a 37 00:02:40,220 --> 00:02:43,800 first name once again. 38 00:02:43,810 --> 00:02:48,430 We could find the existence of this function either by looking at the official documentation or just 39 00:02:48,430 --> 00:02:56,540 command clicking on faker and then search this thing for like first name and there it is. 40 00:02:56,570 --> 00:02:57,740 So here is the name module. 41 00:02:57,740 --> 00:03:00,290 There's the first name method. 42 00:03:00,290 --> 00:03:04,670 Even by looking at this documentation right here or this type definition file you'll notice that we 43 00:03:04,670 --> 00:03:08,350 even are told that we can taken a gender argument right here. 44 00:03:08,360 --> 00:03:10,850 This actually did not exist in the official documentation. 45 00:03:10,850 --> 00:03:16,760 So the type definition file actually gives us more information sometimes than a typedef or semi a documentation 46 00:03:16,760 --> 00:03:20,350 document actually does all right. 47 00:03:20,350 --> 00:03:25,890 So now we've generate the first name we can do the same thing for our latitude and longitude as well. 48 00:03:25,930 --> 00:03:29,800 Now here's a really quick note for you something that really gets people hung up when they are getting 49 00:03:29,800 --> 00:03:36,550 started with typescript at this point we have only tell told typescript that our class user is going 50 00:03:36,550 --> 00:03:43,000 to be of type or going to have a property called location and location will have an object with a latitude 51 00:03:43,030 --> 00:03:46,910 and longitude property here's the important thing. 52 00:03:47,150 --> 00:03:53,550 When we create an instance of a user location does not get automatically set to be an object. 53 00:03:53,600 --> 00:04:00,080 So in other words if we did right here a console log of this dot location we would see undefined or 54 00:04:00,080 --> 00:04:01,330 no. 55 00:04:01,370 --> 00:04:05,990 That means that if we tried to do direct assignment to latitude and longitude like this dot location 56 00:04:05,990 --> 00:04:10,970 dot lat equals you know some number we would actually see an error message and would say something like 57 00:04:11,210 --> 00:04:17,950 cannot read property of left of undefined so all we have done up here is said hey typescript we're going 58 00:04:17,950 --> 00:04:23,890 to have a location property it will be an object that has these two properties but we have not done 59 00:04:24,100 --> 00:04:25,950 any initialization for that yet. 60 00:04:25,960 --> 00:04:29,500 There is no object here when we create our user. 61 00:04:29,500 --> 00:04:36,870 So we are responsible for initialization of this object and the properties inside of it. 62 00:04:36,880 --> 00:04:40,230 So in other words that means to initialize latitude and longitude. 63 00:04:40,240 --> 00:04:49,660 We have to say this dot location is going to be an object with properties lat and LNG so we can try 64 00:04:49,660 --> 00:04:57,450 to initialize both Latin LNG will do you faker dot address dot latitude I'll get a comma at the end 65 00:04:57,450 --> 00:05:04,900 of that line and then faker dot address dot longitude like so even then I still see an error message 66 00:05:04,930 --> 00:05:11,590 if we mouse over this you'll notice that it says expected are right here type string is not assigned 67 00:05:11,600 --> 00:05:15,710 a ball to type no it's remember when we were looking at the documentation for latitude and longitude 68 00:05:15,710 --> 00:05:20,390 or the type definition filed just a second ago we saw that latitude longitude where functions that return 69 00:05:20,390 --> 00:05:27,030 Strings but we clearly said that latitude and longitude were gonna be numbers once again in my opinion 70 00:05:27,030 --> 00:05:32,070 this is kind of an oversight by the faker people and they really should be returning numbers here however 71 00:05:32,100 --> 00:05:33,830 that's just how they built this library. 72 00:05:33,840 --> 00:05:39,060 So we need to take the strings that are returned by latitude longitude and convert them into numbers 73 00:05:39,930 --> 00:05:44,660 to do so we have to do an actual conversion process like this is a string right here. 74 00:05:44,670 --> 00:05:47,940 So we have to convert that into an actual number. 75 00:05:48,140 --> 00:05:53,790 So to do so we're going to take a very javascript style approach we're gonna use the pass float function 76 00:05:54,410 --> 00:05:59,070 just to make sure you are aware of the pass float function is built into javascript it takes a string 77 00:05:59,130 --> 00:06:06,520 and returns a number that has a decimal value attached to it so we can call a pass float on both and 78 00:06:06,520 --> 00:06:12,410 the error goes away because now we are signing a number to both latitude and longitude All right. 79 00:06:12,450 --> 00:06:13,840 That's pretty much it. 80 00:06:13,890 --> 00:06:15,150 Let's take a quick pause right here. 81 00:06:15,290 --> 00:06:18,530 When come back the next video we're gonna start to actually make use of our class. 82 00:06:18,600 --> 00:06:19,950 So I'll see you in just a minute.