However as the console
object is guaranteed to always exist on the global object, the globalThis
part can be omitted, which is what we've been doing in our code samples. TypeScript also comes with built-in type definitions that cover all these built-in features, so it knows they exist and can type check them properly, even though you didn't write any type declarations for them.
Back in part 3 we used String(...)
and Number(...)
to convert between strings and numbers - these are also globals and could also be accessed by globalThis.String(...)
. But once again there's no need as these are built-in.
There are a wide range of globals built-in to TypeScript (most of which are inherited from JavaScript). You can depend on these anywhere you write TypeScript code. There are many more added by browsers, providing a wide range of sophisticated features ranging from networking to accessing peripheral devices like cameras. Other environments such as node.js also add different sets of features. Tools like Construct also provide their own set of features. These sets of features are collectively referred to as Application Programming Interfaces, or APIs. We've covered a very small number so far. Later on this guide will cover several more features that are built-in to TypeScript and so will work everywhere you can write TypeScript, as well as a few browser APIs, and some of the APIs specific to Construct.
Conclusion
In this part we've covered:
- Creating an object
- Accessing object properties
- The types used for objects
- Using strings to refer to object properties
- Nesting objects
- How objects are passed by reference, whereas numbers and strings are passed by value
- Using the global object via
globalThis
There's more to cover about objects, since they're such a fundamental aspect of TypeScript programming. In particular using function properties on objects has some additional unique features. The next part will cover that and some more details about objects in TypeScript.
Learn more
If you want to dig deeper, you can learn more about the features mentioned in this guide at the following links: