Book Image

Hands-On TypeScript for C# and .NET Core Developers

By : Francesco Abbruzzese
5 (1)
Book Image

Hands-On TypeScript for C# and .NET Core Developers

5 (1)
By: Francesco Abbruzzese

Overview of this book

Writing clean, object-oriented code in JavaScript gets trickier and complex as the size of the project grows. This is where Typescript comes into the picture; it lets you write pure object-oriented code with ease, giving it the upper hand over JavaScript. This book introduces you to basic TypeScript concepts by gradually modifying standard JavaScript code, which makes learning TypeScript easy for C# ASP.NET developers. As you progress through the chapters, you'll cover object programming concepts, such as classes, interfaces, and generics, and understand how they are related to, and similar in, both ES6 and C#. You will also learn how to use bundlers like WebPack to package your code and other resources. The book explains all concepts using practical examples of ASP.NET Core projects, and reusable TypeScript libraries. Finally, you'll explore the features that TypeScript inherits from either ES6 or C#, or both of them, such as Symbols, Iterables, Promises, and Decorators. By the end of the book, you'll be able to apply all TypeScript concepts to understand the Angular framework better, and you'll have become comfortable with the way in which modules, components, and services are defined and used in Angular. You'll also have gained a good understanding of all the features included in the Angular/ASP.NET Core Visual Studio project template.
Table of Contents (16 chapters)

Generic functions and interfaces

The example at the end of the Inheriting from a class and implementing interfaces section of Chapter 4, Using Classes and Interfaces, uses the following interface:

interface ItemList {
appendItem(itemData: any): void;
prependItem(itemData: any): void;
appendBefore(node: HTMLElement, itemData: any): void;
appendAfter(node: HTMLElement, itemData: any): void;
removeItem(node: HTMLElement): void;
itemTemplate: (x: any) => HTMLElement;
readonly itemsParent: HTMLElement;
}

Various methods accept an itemData parameter, which represents a general data item whose fields have to be be used to assemble new elements to add to the DOM list represented by the interface. Its type is any since the data item may have any type. However, by using any, one misses several opportunities:

  • The type of itemData can be any type but it is exactly the...