Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering TypeScript - Fourth Edition

You're reading from  Mastering TypeScript - Fourth Edition

Product type Book
Published in Apr 2021
Publisher Packt
ISBN-13 9781800564732
Pages 538 pages
Edition 4th Edition
Languages
Author (1):
Nathan Rozentals Nathan Rozentals
Profile icon Nathan Rozentals

Table of Contents (19) Chapters

Preface 1. Up and Running Quickly 2. Exploring the Type System 3. Interfaces, Classes, Inheritance, and Modules 4. Generics and Advanced Type Inference 5. Asynchronous Language Features 6. Decorators 7. Integration with JavaScript 8. Strict Compiler Options 9. Using Observables to Transform Data 10. Test-Driven Development 11. Angular 12. React 13. Vue 14. Node and Express 15. An AWS Serverless API 16. Micro Front-ends 17. Other Books You May Enjoy
18. Index

Generics and Advanced Type Inference

Thus far, we have been exploring the type system within TypeScript, and how it relates to interfaces, classes, and primitive types. We have also explored how to use various language features to mix and match these types, including type aliases and type guards. All of the techniques we have used, however, eventually boil down to writing code that will work with a single particular type. This is how we achieve type safety within TypeScript.

But what if we would like to write some code that will work with any sort of type, or any sort of interface or class definition? Perhaps a function that needs to find an element in a list, where the list could be made of strings, or numbers, or any other type. This is where generics come into play. Generics provide a mechanism to write code that does not need to specify a specific type. It is left up to the caller of these generic functions or classes to specify the type that the generic will be working with...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}