TypeScript for Angular Developers: topics
Understanding the Role of TypeScript and Transpilation
We will look at how the TypeScript compiler produces JavaScript code. We will dig into compiler options, and gain an understanding of the key pieces of the typescript compiler configuration (tsconfig.json). Transpilation is a mashup of “translation” and “compilation”: it means that we have one language (in this case TypeScript), that when put through the compiler, produces another language (here, JavaScript). Sometimes our TypeScript code produces the exact same code as the JavaScript, sometimes it produces markedly less JavaScript code than our TypeScript source, and sometimes even more. We’ll understand how this process works. We will also explore type declaration (.d.ts) files and how TypeScript uses them when integrating with third-party libraries.
Use Object-Oriented Programming in TypeScript
We will start on what, for many developers, is somewhat familiar ground. We will learn how to create types using classes, how to design constructors, methods, and properties. We will explore common OOP idioms like encapsulation, inheritance, and polymorphism. Finally, we will explore more advanced OOP patterns like factories, and dependency injection and see how TypeScript makes these patterns even easier than many statically typed languages (like C# or Java).
We will, in this section, explore the “limited” primitive types in JavaScript/TypeScript, and later we will learn to extend and customize them.
Use Functional Programming in TypeScript
While JavaScript (and therefore TypeScript) isn’t a strictly functional language like Lisp, Scheme, or Haskell, it does lean heavily into functional programming styles. We will learn how to solve the same old problems (and some new ones!) we’d solve with classes by composing our solution with a set of related functions. We will learn about higher-ordered functions (functions that take one or more functions as an argument, and/or return a function), explore immutability, and learn how functions can be typed, and provide elegant solutions to common web programming tasks.
We will explore why the functional, immutable style of TypeScript programming (that often feels alien to developers coming from C# or Java) is a perfect fit for applications that run in a web browser (like Angular).
Defining Our Own Types
This is the major shift in our thinking that TypeScript demands. As C# or Java programmers, “Types” are a sort of side-effect of creating classes. In languages like TypeScript, we have the most success when we think about the kind of things we need (types), and then provide the code that accomplishes our intentions.
TypeScript has a rich set of features for defining, extending, and even limiting existing types. We will learn the built in type utilities (like Pick, Omit, etc.), learn to define types with interfaces and type aliases, explore union and intersection types, solve common programming issues with discriminated unions, learn type narrowing and type assertions, and learn the difference between the any type and the unknown type.
We will learn how to implement generic types, so that we can easily reuse type definitions to create order in our code base.
Then we will take it to the next level and learn how TypeScript allows us to create new types from existing types. Using Mapped Types and Template Literal Types, we will do things that you just can’t do in statically typed languages like C# or Java - we will have our TypeScript code create new types for us, opening up a new world of possibilities, particularly for library or framework developers.
Organizing TypeScript Code within an Application
A nice thing about TypeScript being a superset of JavaScript is the ability to do gradual typing. As application developers, TypeScript will largely stay out of our way, and infer types for us. Even with minimal type annotations, the TypeScript compiler will replace literally hundreds of unit tests we used to have to write if we were writing “raw” JavaScript. We can decide to add additional type information to both better document our code, as well as protect other developers on our project when they decide to reuse something we’ve created and tested.
In this section we will divert a bit into talking about JavaScript modules, bundlers, and how the code is built to send to the browser, then explore patterns like exports, imports, and “barrels” within our project. The TypeScript compiler provides some early-bound, compile time protection against changes in monolithic applications, and we will optimize our code to provide the best developer feedback.
Packaging TypeScript Libraries For Others with NPM
When we have some TypeScript code that graduates from being specific to a project, to more generally useful across other projects, we might consider creating a reusable package for our code. We will learn how to compile, bundle, and make our dependencies explicit in these packages, as well as providing human documentation, as well as the type declarations, for other developers. We will implement Semantic Versioning and provide tests that help us decide when a part of the version (major, minor, or patch) needs incrementing.
We will also learn good dependency “hygiene” - the role of package.json, package-lock.json, and how to update and maintain compatibility with versioning in a project where you have several developers working on the code, and want to ensure the dependencies in your final build are the “blessed”, approved dependencies.
TypeScript Can’t Do Everything
As great as TypeScript is, it has its limitations. TypeScript developers must be aware that no matter how expressive of a type system you create for your application, it does not exist at the level of the JavaScript code running in the browser or NodeJS. Many TypeScript programmers are shocked when their well-designed code fails (sometimes spectacularly, sometimes slowly “rotting” as the application runs) when your code encounters unverified code from the “outside” world, like malformed data from an API. We will explore how to validate and ensure the sanctity of our types with type assertions, and take a look at schema validation tools that are essential for TypeScript developers. In this course, we will learn the basics of the Zod schema validation library.
TypeScript Patterns in Angular
We will learn the idiomatic use of TypeScript in Angular applications. We will explore Angular and 3rd-party libraries to see how the patterns from the class are used in these libraries.