Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On TypeScript for C# and .NET Core Developers

You're reading from   Hands-On TypeScript for C# and .NET Core Developers Transition from C# to TypeScript 3.1 and build applications with ASP.NET Core 2

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher
ISBN-13 9781789130287
Length 460 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Francesco Abbruzzese Francesco Abbruzzese
Author Profile Icon Francesco Abbruzzese
Francesco Abbruzzese
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Introduction to TypeScript FREE CHAPTER 2. Complex Types and Functions 3. DOM Manipulation 4. Using Classes and Interfaces 5. Generics 6. Namespaces and Modules 7. Bundling with WebPack 8. Building TypeScript Libraries 9. Decorators and Advanced ES6 Features 10. Angular ASP.NET Core Project Template 11. Input and Interactions 12. Angular Advanced Features 13. Navigation and Services 14. Assessments 15. Other Books You May Enjoy

Adding TypeScript to your web projects

Once you have the TypeScript SDK installed, adding a TypeScript code file to an ASP.NET Core project is straightforward. Your TypeScript file will be compiled in a JavaScript file that you may call, as usual, in any View. However, you may debug your code and put breakpoints directly in the TypeScript source.

Your first TypeScript file

Let's open Visual Studio and create an ASP.NET Core project 2.x named TypeScriptTests:

Click OK, and in the new window that appears, select an MVC 2.x application with No Authentication (we need just a View for where to call our code):

Once the project finishes loading, run it to verify that the project has been scaffolded properly. Then, in the solution explorer, right-click on the wwwroot node and select Add | New Folder to add a new folder named ts:

Finally, right-click on the ts folder and select Add New Item. The following window opens:

In the left-hand menu, select ASP.NET Core | Web | Scripts. Then select the TypeScript File and name it tests.ts.

Let's add some test code to the newly added test.ts file:

var firstName: string = "Francesco";
var surName: string = "Abbruzzese";

function fullName(x: string, y: string, spaces: number): string {
return x + Array(spaces+1).join(' ') + y;
}

alert(fullName(firstName, surName, 3)+" Hello");

Thanks to declarations and strong typing, the Visual Studio editor helps us with IntelliSense:

As soon as we save the file, Visual Studio invokes the TypeScript compiler to transpile our file into a test.js JavaScript file:

In the case of errors, the JavaScript file is not generated and all errors are displayed in the editor as soon as we save the file. Let's try this behavior by misspelling the join method:

When we build the project, all TypeScript errors are also added to the Error List panel, as follows:

Running and debugging TypeScript code

Running and debugging TypeScript code is straightforward. It is enough to add the transpiled JavaScript file to a view. Let's add our previous test.js file to the bottom of the Home/Index.cshtml view:

@section Scripts{ 
<script src="~/ts/tests.js"></script>
}

It is worth remembering that Script is a section defined in the Layout view of the ASP.NET Core MVC template. It allows all views to place the JavaScript code they need in the right place in the Layout view. When you run the project, an alert box should appear as soon as the website's default page is loaded in the browser, as shown in the following screenshot:

Thanks to the test.js.map map file generated by the TypeScript compiler, it is possible to debug the TypeScript source instead of the JavaScript transpiled code. In fact, map files contain all the information needed to map each position in the transpiled file into a position in the source file. Map files are not a peculiarity of TypeScript but a well-recognized standard, since they are also used for mapping minimized JavaScript files to their sources.

Let's place a breakpoint on the last instruction of test.ts and run the project:

Once the breakpoint is hit, you may benefit from all the Visual Studio debugging features you are used to when debugging C# code.

You may see values by hovering over variables with the mouse:

Or, you can use a Watch 1 window:

You also have access to the calls stack, to an immediate window, to the intellitrace, and to all other C# code features you are used to.

You have been reading a chapter from
Hands-On TypeScript for C# and .NET Core Developers
Published in: Oct 2018
Publisher:
ISBN-13: 9781789130287
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 €18.99/month. Cancel anytime