Enjoy free content straight from your inbox 💌
00:00
Here we have a JavaScript variable called center that we have initialized to a JavaScript object with the members X and Y, both pointing to the number zero. Now, the TypeScript syntax for annotating objects is very similar to the syntax of JavaScript object initializes. Here we are saying that center is a variable that must always be assigned an object with the member X of type number and a member Y of type number. Now, we might have multiple variables within our code that all follow the same structure. For example, here we have another variable called unit
00:35
that has the same structure as the variable center. Now, instead of having to copy these inline types again and again, TypeScript allows us to define and reuse types by using a type alias. The general syntax for creating a type alias is to use the type keyword followed by the name. You want to give the type here recalling it point, then the assignment operator followed by anything that can be used in a type annotation. Now, once we have the type alias created, we can use it in any of our type annotations so we can replace what we had in line previously
01:10
by the name of the type alias. Now, using a type alias has two advantages. One, it allows us to name our intent. So our intent for these objects is to represent a point in space, and that is represented by the name of the type. Second, it allows us to remove code duplication resulting in greater code maintainability in the long run.