In the last video we installed a typed definition file for faker as a quick side.
One of those benefits of type definition files is that we can now hover over faker right here and then
if you're on Mac hold down the command key or if you're on Windows hold down control.
You'll notice that faker turns into a clickable link of sorts.
If we now click on that we'll be taken to the typedef definition file that we just installed.
Notice how the name of this file is index dot D dot t s traditionally type definition files are named
with extensions of D dot T.S..
So anytime you are looking at some typescript code and you see a file with that extension that means
you are looking at a type definition file inside this file.
You're going to find nothing but typescript syntax.
So the only goal of this file right here is to describe the different types of values functions objects
classes that exist inside the faker library.
So there will not actually be any functions that are implemented inside of here.
Instead you instead you will only see a definition of the different types that are available if you
look inside of here we can actually use this as a source of documentation.
So for example remember at the official documentation we looked at just a moment ago we saw that address
module.
So this right here is telling typescript that this library has a address object and inside of it are
different functions like zip code city city prefix and so on.
We can see the different arguments that each of them take and a different type of values that they return.
So like I said we can use this file as kind of an alternate forms of documentation to understand how
to work with a given library so in this case we can take a look at address.
If we scroll down you'll notice the latitude and longitude are two functions inside there.
We're gonna use those functions to actually randomly generate our latitude longitude as a quick side
note I want you to notice how latitude longitude return Strings which is actually kind of not very typical
usually we reflect latitude and longitude as numbers.
So having them being returned here as a string means that might be a little bit unexpected and we might
have to deal with that at some point in time but we'll just see what happens.
All right I'm going to close this file and back inside of my user dot t s file.
We're not going to do some initialization of our name.
Latitude and longitude inside of our constructor.
So inside the constructor as soon as we make an instance of a user we're going to randomly generate
a name and assign it to that name property.
So inside of here I'll say this dot name is going to come from faker.
We're going to get the name module.
Remember we saw that in the official documentation his name right here and we're going to generate a
first name once again.
We could find the existence of this function either by looking at the official documentation or just
command clicking on faker and then search this thing for like first name and there it is.
So here is the name module.
There's the first name method.
Even by looking at this documentation right here or this type definition file you'll notice that we
even are told that we can taken a gender argument right here.
This actually did not exist in the official documentation.
So the type definition file actually gives us more information sometimes than a typedef or semi a documentation
document actually does all right.
So now we've generate the first name we can do the same thing for our latitude and longitude as well.
Now here's a really quick note for you something that really gets people hung up when they are getting
started with typescript at this point we have only tell told typescript that our class user is going
to be of type or going to have a property called location and location will have an object with a latitude
and longitude property here's the important thing.
When we create an instance of a user location does not get automatically set to be an object.
So in other words if we did right here a console log of this dot location we would see undefined or
no.
That means that if we tried to do direct assignment to latitude and longitude like this dot location
dot lat equals you know some number we would actually see an error message and would say something like
cannot read property of left of undefined so all we have done up here is said hey typescript we're going
to have a location property it will be an object that has these two properties but we have not done
any initialization for that yet.
There is no object here when we create our user.
So we are responsible for initialization of this object and the properties inside of it.
So in other words that means to initialize latitude and longitude.
We have to say this dot location is going to be an object with properties lat and LNG so we can try
to initialize both Latin LNG will do you faker dot address dot latitude I'll get a comma at the end
of that line and then faker dot address dot longitude like so even then I still see an error message
if we mouse over this you'll notice that it says expected are right here type string is not assigned
a ball to type no it's remember when we were looking at the documentation for latitude and longitude
or the type definition filed just a second ago we saw that latitude longitude where functions that return
Strings but we clearly said that latitude and longitude were gonna be numbers once again in my opinion
this is kind of an oversight by the faker people and they really should be returning numbers here however
that's just how they built this library.
So we need to take the strings that are returned by latitude longitude and convert them into numbers
to do so we have to do an actual conversion process like this is a string right here.
So we have to convert that into an actual number.
So to do so we're going to take a very javascript style approach we're gonna use the pass float function
just to make sure you are aware of the pass float function is built into javascript it takes a string
and returns a number that has a decimal value attached to it so we can call a pass float on both and
the error goes away because now we are signing a number to both latitude and longitude All right.
That's pretty much it.
Let's take a quick pause right here.
When come back the next video we're gonna start to actually make use of our class.
So I'll see you in just a minute.