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: