Skip to content
learn

TypeScript for C#/Java developers — Mission

The front ends of Guitar Alchemist are written in TypeScript, and so are the configuration files of this site. I read that code as a C# developer, and it looks like C# with the types in the wrong place, until it doesn’t: an object is accepted where a class was expected, a cast converts nothing, a generic has no T at run time, and a program with type errors runs anyway. The JavaScript course covered what happens at run time. This one covers what tsc checks, what it can’t, and where the two meet.

You are comfortable with C# or Java, including generics, interfaces and nullable reference types or Optional, and you have followed the JavaScript for C#/Java developers course, or know its content: modules and npm, values and conversions, functions and this, prototypes and classes. The lessons link to its pages instead of explaining JavaScript again.

Components, JSX and the DOM belong to the React (Vite) course that follows this one. This course runs TypeScript in Node.js, where every example can be checked and run in CI, and looks at React code only as data from the GA repository.

The Advanced TypeScript course goes further for developers who already write strict TypeScript: how the checker evaluates the types computed from other types, variance, modeling with brands and state machines, and schemas at the border of a program.

C# Java TypeScript
Compiler csc, through dotnet build javac tsc, which checks and can write JavaScript
Project file .csproj pom.xml, build.gradle tsconfig.json, next to package.json
Types at run time reified: typeof(T), is T erased generics, checked casts none: every type is erased, casts included
Compatibility nominal: declared inheritance nominal structural: the same shape is the same type
A type error stops the build stops the build stops nothing unless the build asks tsc
Absent value string? with warnings Optional<T>, annotations string | undefined, errors under strictNullChecks
One of several cases class hierarchy, switch with patterns sealed interface, switch with patterns union types and narrowing
Generics reified, variance on interfaces erased, wildcards erased, structural variance, types computed from types

The lessons use real code as material. GuitarAlchemist/ga is quoted at commit a826864, in its two React front ends, ReactComponents/ga-react-components and Apps/ga-client, which I installed and checked with tsc 5.9.3 and 7.0.2. This site, built with Astro, is the other TypeScript project, at commit 8ba378e. The lessons reduce the lines they quote to files that run on their own, and say where each one comes from. What tsc found in those projects is in the journal.

By the end of this course, I will be able to

Section titled “By the end of this course, I will be able to”
  • set up a TypeScript project for Node.js, read its tsconfig.json, and make type errors fail the build;
  • read a tsc diagnostic and relate its code to what C# or Java would have said;
  • predict when two types are compatible, and give a type a name when structure isn’t enough;
  • model data as unions, narrow them, and get a compile error when a case is missing;
  • check data from outside the program at the border, instead of asserting its type;
  • write generic functions and types, and explain why new T() and instanceof T don’t exist;
  • compute types from other types with keyof, mapped and conditional types;
  • type a JavaScript library, a Node.js package and a front end built with Vite, and migrate a JavaScript project to strict.
# Lesson You already know
1 The compiler and the tooling csc, javac, .csproj, compiler warnings and errors
2 Structural typing interfaces, var, readonly/final, object, dynamic, nullable reference types
3 Unions and narrowing class hierarchies, switch with patterns, sealed interfaces, casts
4 Generics List<T>, constraints, IEnumerable<out T>, wildcards, type erasure
5 Type-level programming: conditional types, infer, template literal types and satisfies (coming next) overload resolution, typeof, attributes
6 Classes in TypeScript: modifiers, abstract, implements, decorators classes, access modifiers, attributes and annotations
7 Modules and declaration files: .d.ts, @types, declare module, typing JavaScript with JSDoc reference assemblies, extern, JAR metadata
8 Asynchronous code and errors: Promise<T>, typed errors, result types Task<T>, exceptions, CompletableFuture, checked exceptions
9 Data at the border: fetch, JSON, schema validation and types generated from OpenAPI System.Text.Json, Jackson, NSwag, OpenAPI Generator
10 Testing code and types: Vitest, @ts-expect-error, type tests xUnit, JUnit, analyzers
11 Projects at scale: project references, tsc -b, monorepos, typescript-eslint solutions, multi-module Maven builds, analyzers
12 Migrating to strict: GuitarAlchemist/ga’s front ends, and TypeScript 6 to 7 enabling nullable reference types on an existing project
— Journal