We're almost done with the basics about TypeScript and the core types.
There are two more types which are good to be aware of because they will matter from time to time.
The first type is the UN known type for Dad all named does fall here functions T S and add a new apt
yes file and in their let's say we have a new variable user input and this is of type and known.
It's not type any which would be the default but unknown which is a different type introduced by typescript.
It might be unknown because we don't know yet what the user will eventually enter if it's a number if
it's a string.
We don't know.
Now the interesting thing about the unknown type is we can store any value in there without getting
errors.
So this is all allowed.
If I now compile my file here you see I get no compilation error so thus far it's the same as if we
wouldn't have assigned a type here and hence we have any type.
Or if he would have explicitly set any as a type but still unknown is different to any this year works
but will run into issues if I have another variable let's say user name which should be a string and
that's on initialized here but then here I want to set a user name equal to user input.
Now you see we're getting an error that type unknown is not a sign to type string.
So user name once a string and of course unknown is not guaranteed to be a string.
Here I assigned one but that's just a case in this line.
User input technically could hold any value because it's I'm known.
Now the interesting thing is if I switch unknown to any this error goes away because any is the most
flexible type and TypeScript and it basically disables all type checking and typescript just says I
give up.
Do whatever you want unknown is a bit more restrictive than any with unknown.
We have to first of all check the type that's currently stored in user input before we can assign it
to for example a variable that wants a string.
So since a string is wanted here we could check if type of user input is equal to string and typescript
will detect this check and understand that in here what is stored in user name because it's inside of
this if statement where I check for a user input being of type String user input is guaranteed to be
a string and Dale for I can safely assign to user name so I need such a extra type check here with unknown
to be able to assign a unknown value to a value with a fixed type and Dale for unknown is the better
choice over any if you know I can't tell exactly what type all stored in there it might be a number
it might be a string but I know what I want to do with it eventually we'll just add an extra check to
make sure that what you want to do can be done so that if you want to work with the string you have
a string and you're good and then unknown is better than any because it makes sure that you're not allowed
to do everything but you have at least some type checking.
Of course if you have a chance of knowing in advance that user input is always a string or always a
string or a number well then you should use string or such a union type instead of unknown so unknown
still is a type you shouldn't use all the time but it is better than any for the reasons described.