In this video I want to make one additional change to our program.
I want to take this consular statement and extract it into a separate helper function that will allow
us to leave this promised body right here a little bit more compact than it currently is.
So to get started I'm going to define a new function at the bottom of the file.
I'm gonna call it log to do I'm going to set this up as an aero function and I'm going to say that when
we call this function we should take in the arguments I.D. title and completed.
If you see a little nasty underline right here like a yellow or even a red underline that's totally
fine.
Just stick with it for a moment.
Once I define that function I'm going to console irks me.
Cut and paste the console log inside there.
Like so so now we can call that function room up here and that will do our console log for us so I'm
going to call log to do and I'll pass in the I.D. that completed flag and the title like so.
Now once again if you're looking at this and you think thinking Steven you just made an error there.
That's totally fine.
Once again just bear with me for a moment.
So I'm not going to save this file and I'm going to flip back or my turtle and let's try to run this
once more so I'll do a test node index dot t yes in my program runs but if you look carefully the console
log is not quite right.
So it says has a title of false.
Is it finished elect us ot Autumn.
Hmmm something's definitely off here.
So what do we do wrong.
If I flip back over and look very carefully you'll notice on my log to do function I was expecting arguments
in the order of I.D. and then the title and then completed.
However when I called the function I passed an I.D. then completed and then title so I clearly mixed
up the order of arguments into the function and once again I got absolutely no information about the
error that I just made until I actually executed my code at the terminal.
So once again here's the lesson about typescript if we're not using typescript correctly or for just
writing out plain javascript we don't find errors until we run our code which is pretty inefficient
from a developer perspective.
So if we used typescript appropriately we could have gotten a warning about this or some error inside
of our Ed before we ran our code and that would have been a signal to you and me that we had to fix
up something.
So how are we gonna fix this up.
Well I'm going to find my log to do function right here and on each of these arguments I'm going to
add in a type annotation.
So we've used that term several times so far.
Remember we're going to really clarify all these typescript topics in just a moment.
So right now just bear with me.
We're going to add a little bit of typescript syntax here.
So going to say that this idea variable whatever we pass in here it has to be a number and then our
title it has to be a string number that makes sense our title is going to be a string and that completed
a flag.
It has to be a boolean either true or false.
So we've now written out IDI colon number and then the same thing for the other two arguments as well.
Now this is not creating an object or anything like that.
Instead we are trying to describe the type of variable that idea is going to be the type of variable
that title is going to be and the same thing for completed as well.
Now as soon as I add that in I can then see a red underline under the completed flag right here.
Now if I hover over it it will tell me argument of type boolean is not a sizable 2 parameter of type
String.
So what this is telling me is that completed is supposed to be a boolean.
Right.
That's what this is completed is a boolean it is a true or false value.
And I tried to pass it in as the second argument to the log to do a function but log to do says the
second argument has to be a string a boolean is not a string and that's why we are seeing this error.
So once again this is a notice to you and I as the developers that were doing something incorrectly
and we have to fix this up.
So in this case I would probably analyze this and say oh yeah I just made a mistake in the order of
arguments here.
So I would flip the order around like so and then the error message goes away.
And now I can save this file.
I can flip back to my terminal again run the file once more and now I'll see the correct result.
All right so that's it.
Two quick examples of why we like to use typescript.
We just saw two examples of extremely common errors like people making typos on object properties that
happens every day probably thousands or even tens of thousands of times a day across the world with
JavaScript and we also saw an example of calling a function with the incorrect order of arguments.
Also an extremely common error in both cases we would have had to run our code to find these errors
but by using a tiny amount of typescript we found these errors during development and we saved ourselves
a lot of time.
So we've now seen some very basic examples of why we'd like to use typescript.
Let's take a quick pause right here.
We're gonna start working on our next application in the next video.