Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, and any other types that you’d otherwise have to write by hand.
String literal types allow you to specify the exact value a string must have. In practice string literal types combine nicely with union types, type guards, and type aliases. You can use these features together to get enum-like behavior with strings.
Union types are closely related to intersection types, but they are used very differently. Occasionally, you’ll run into a library that expects a parameter to be either a number or a string. A union type describes a value that can be one of several types. We use the vertical bar (|) to separate each type, so number | string | boolean is the type of a value that can be a number, a string, or a boolean. If we have a value that has a union type, we can only access members that are common to all types in the union.
Enums are global constraint identifiers. They allow us to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums
This code sample shows how to create and iterate through arrays in Typescript.
As seen in the previous example, we see how we create functions. We provide the function keyword and the name of the function. If parameters exist, we should specify the type of them after the variable and a colon.
The types of the parameters in Typescript should be specified after a colon. An example is shown in the code where the variables, x and y are specified as strings.
We have created variables of type number, string and boolean on lines 2-6. We have created them with the let keyword, meaning they can be modified. On lines 8-12, we've created the same type of variables preceded by const, meaning they have to stay constant and cannot be changed.
The typeof operator returns a string representing the data type associated with the object.