TypeScript object : The type you've never used, or have you?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's lesson we will look at a typescript type that i've never used well directly now javascript has a number of built-in types for example for boolean number and string and then the two special ones which are null and undefined and now modern javascript also has two more primitives which are symbol and big int as far as javascript documentation goes everything else is this thing called an object as examples of objects we can have a car ford mustang and a person john of age 23. now as a thought experiment consider this variable i take non-primitives only and your mission should you choose to accept it is to only allow objects to be assigned to this particular variable so if we assign it a person or a car it should be fine but any of the primitive types should result in a compile time error now there is a native type within typescript specifically designed for this particular purpose and that type is called object with a lowercase o this type will allow any object to be assigned to it other than the primitive ones that exist within javascript so assigning an object like person or a car is fine but anything primitive is going to result in a compile time error and this is exactly what we wanted now you might be wondering why you would want to use something like this and you are perfectly correct i haven't used the explicit object annotation ever even a single time in my life but let's look at another example you still have these objects representing a car and a person now if you wanted to maintain a map that goes from any given javascript object to its financial value you can do that with the built-in modern javascript weak map data structure so for the car ford mustang we can give it the financial value 50 000 and for the person john we can give it the value infinity as all human life is priceless however assigning a monetary value to a primitive number such as 456 is not something that is really useful and fortunately we get an error automatically in this particular case from the weak map data structure and this is something that is very intentional if you look at the documentation of weak map on mtn you can see that mentioned over here that primitive data types as keys are not allowed so number being a primitive type is not allowed as a key for weak map and this is pointed out by typescript with the error message argument of type number is not assignable to a parameter of type object notice the use of the built-in type object for keys of the weak map without having the concept of this built-in native type typescript would not be able to correctly model the requirements of the weak map so even though i haven't used the object annotation explicitly by using something like weak map i am implicitly depending upon this particular type within type script and it might end up showing within error messages in your code base and weak maps are actually used quite commonly even for example within facebook react now weak maps are great as they do not prevent garbage collection if a particular value goes out of scope and is only present within the weak map and that is really the key objects are something that do go out of scope whereas primitive values are not something that go out of scope as you can always get a handle to one two three by just writing it in now the object type annotation is actually used by a few other annotations within the typescript built-in type definitions as well but peak map is a good example to work from to give you a general understanding of the requirement to have something like this if you enjoyed this lesson smash that like and subscribe for more content like this and i will see you in the next one
Info
Channel: basarat
Views: 10,272
Rating: undefined out of 5
Keywords: typescript, javascript, basarat, that typescript guy, typescript deep dive, react, node, npm, angular
Id: 0y5hhzuQjCA
Channel Id: undefined
Length: 3min 42sec (222 seconds)
Published: Sun May 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.