You know C# well, and a lot of TypeScript will feel familiar: classes, lambdas, generics, ?. and ??. But some things are fundamentally different, and that’s exactly where C# developers make their mistakes. So this lesson doesn’t go through the whole language, only what you need for the Kladde’s scripts, with an eye on the differences from C#.

The playground

For the examples of this lesson and the next, you create a small web project in your practice folder k2-practice. It’s set up like Kladde.Web in lesson 2.2.1:

Bash
dotnet new web -o playground
cd playground
dotnet package add Microsoft.TypeScript.MSBuild --version 7.0.1

Copy the tsconfig.json from src/Kladde.Web/Scripts in the Kladde to playground/Scripts/tsconfig.json, and add the same two settings and the entry for the tsconfig.json to the project file as in Kladde.Web. The playground doesn’t need the target CompileScriptsFirst, because you never publish anything from it. Program.cs only serves files:

C#
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();

app.Run();

Plus wwwroot/index.html:

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Playground</title>
  <script type="module" src="js/playground.js"></script>
</head>
<body>
  <main>
    <h1>Playground</h1>
    <p>The output appears in the console of the browser tools (F12, “Console” tab).</p>
  </main>
</body>
</html>

You write the examples one after another in Scripts/playground.ts. Start the playground with dotnet run, open the address it names and open the console. After every change you quit with Ctrl+C, start again and reload the page. What console.log prints appears in the console; what the compiler reports appears in the terminal, and then the playground doesn’t even start.