Whenever you and I write typescript code we're going to rely upon type inference quite heavily.
This is where typescript is going to figure out the different types that are flowing around our application
for us.
However there are three different scenarios that we're going to look at where we're going to have to
add and type annotations manually instead remember these type annotations are going to help typescript
along the first example we're going to look at is whenever we have a function that returns the any type
and we need to clarify it and we haven't really discussed the any type just yet so we're gonna write
out first just a little bit of code.
Then we'll review what the any type really means and we'll get a better idea of why we need to use a
type annotation.
In this case.
So let's get to it.
I'm gonna flip back over to my code editor and then I'm going to go down to the very bottom of the file
down here we're going to add in are three different examples on type annotations so I can put in a little
comment that says when you use annotations so Case Number one is when we have a function that returns
the any type okay.
So for this example I want to define a little snippet of Jason as a string number just going to try
to pass that Jason.
So I'm going to define a new variable called lowercase Jason and I'm going to assign it a Jason string
so I'll put down a string and an inside of here we're gonna write out just a little bit of Jason so
I'll say curly brace I'll put down X in double quotes a colon and then 10 then y in double quotes a
colon and 20 and then I'll close off the curly brace like so.
So now I could use that string with the Jason pass function and turn the string into an actual object.
So I'm going to create a new variable called about coordinates and I'll assign it the result of Jason
that pass with that Jason variable.
So in theory if we now round this code and console logged out coordinates we would see something like
X is 10 and Y is 20.
Like so now there's just one little gotcha inside of here.
If you mouse over a coordinates you'll notice that it has a type annotation of any.
So this means that typescript thinks that coordinates is of type any.
In addition if we mouse over pass right here you'll notice a lot of nasty syntax up here but at the
very end of it is something that says colon any.
That means that the Jason parts function returns the any type so clearly this anything is something
pretty important.
Let's have a quick pause a little aside here and discuss what the any type means and then we'll understand
based on that discussion why we would need to add in a type annotation here even though it looks like
there's no errors or warnings with our code.
All right.
So quick aside here.
So the first thing I want to review is how do adjacent parts function works in this diagram.
I've got some different values on the left hand side and we're going to imagine that we feed each of
these different values into Jason pass and we'll take a guess at what the different types are that will
come out of the function.
Now in reality this diagram is just a little bit misleading right here.
In reality any time we call Jason not pass we get back a value of the any type.
So this column over here on the right hand side.
I'm just saying this is what the like.
Maybe the type would be right.
This is like we know that the type of false ones we pass it would be a boolean.
That's not what we get with typescript.
This is just like the ideal world so if we fed in a string of false to Jason not pass we get back a
boolean.
Again that's not what really happens we get back any but hey we're just saying we get back a billion
if we pass in a string of four we would get back a number if we pass in something that looks like an
object with value and then five we would get back a type that is an object with value that is a number.
And likewise if we have a object with name that is pointing out a string we would get back an object
with a name property that is a string and you can see each these examples very easily if you open up
a javascript console really quickly and you call say Jason dot pass with a string of like five we get
back a number right.
Makes sense.
Now the kind of common theme here is that we can clearly pass in very different strings into this function
and we can possibly get back very very different value types in some cases we might get a billion.
In other cases we might get a number.
And then in some other cases we might get these really complicated objects so the goal of this diagram
right here is just to help you understand that anytime we call Jason dot pass we get back possibly some
very very different value types.
Now the problem here is that typescript can not predict these different types typescript doesn't know
what we're going to get out of Jason dot pass because it depends entirely on the string that we put
into that function so when you and I are writing code inside of our code editor and we call Jason dot
pass typescript just plain can't predict what we get back from the function because it will be entirely
different based upon the string that we put in and typescript is not going to try to like read the string
and figure out what it would return if we called Jason dot pass.
So as a shortcut type script instead decides to simply say you know what I can't guess all these different
types that's too complicated.
So I'm going to say if you ever call Jason dot pass you're going to get back something called the any
type the any type essentially means that typescript has no idea what type of value is being returned
from Jason that pass has no clue at all.
And that's what we see inside of our code.
Ed so right here when we mouse over coordinates typescript is saying this is the any type.
That means that typescript has no idea what type coordinates is so that's what the any type indicates.
Now a couple more notes here on the any type so any is a type just as string or boolean are.
Any time we have to write out a type of a variable like for example in a type annotation we could in
theory put in any just as easily anytime we see something that is marked with the any type that means
that typescript has no idea what's going on.
And in general that is a very bad thing to have inside of application.
Remember the entire idea behind TypeScript and all these types and all this kind of stuff is that we're
using typescript to catch errors inside of our code Ed and we're able to do that because of these types
like types like string and number and boolean and whatnot.
If typescript knows the different types inside of application it can take a look at any time we say
call a function on an object or reference a property on an object and type again can figure out whether
or not we are referencing a property that actually exists or if it's one that we just made a typo on.
So anytime we have a value that is the any type typescript can not do any error checking whatsoever
around that value.
So for example right now if I go back over to my code editor I can write out coordinates dot blah blah
blah blah blah like so this is clearly a property that does not exist but you'll see that typescript
does nothing to help me out and tell me that I might have a mistake right here.
However if I go back up to any of these other examples where I'm working with variables that have known
type like let's say how about speed right here if I reference speed dot blah blah blah typescript is
going to jump in very quickly and say hey that's not a property that exists it's only going to allow
me to call properties that actually exists like let's say two upper case like so that's fine.
And it's only fine because typescript knows that speed is a string.
So again anytime we have a variable of type any it is generally a bad thing because typescript can't
do its job.
All right.
So I think that we've gone into enough detail on this.
Any type.
So let's have another quick pause right here.
We're going to come back the next video and understand how we can use a type annotation to fix this
thing up right here so quick pause and I'll see you in just a minute.