04 / TypeScript
Types as contracts
TypeScript does not run in the terminal here. It is a contract you write so a later reader cannot smuggle the wrong shape through.
TypeScript · 14 min · 0/5 quiz strings accepted
A type is a promise
When a lesson object must have a slug and a title, write that down. The compiler then refuses a half-built record before a learner ever sees it.
Keep types close to the data they describe. A Lesson type that lists sections, quiz items, and hints is easier than a grab-bag of any.
type Lesson = {
slug: string;
title: string;
quiz: QuizItem[];
};Narrow the outcome
The practice terminal has two legal results: ACCEPTED or REJECTED. A union type makes a third string a compile error.
Functions should return the union, not string. That keeps the UI from inventing a third banner.
type Verdict = "ACCEPTED" | "REJECTED";
Optional is a decision
starter? on a quiz item means the prompt can stand alone. Do not mark everything optional to silence errors. Missing data should be visible.
The report’s first TypeScript exercise
Anders Hejlsberg designed TypeScript at Microsoft in 2012 so large JavaScript systems could carry types that erase before the browser. Adoption sits between 43.6% and 51.4%. The beginner exercise is a User interface — name as string, age as number. This terminal will not render a greeting.
interface User {
name: string;
age: number;
}Glossary
- union
- A type that allows one of a listed set of values.
- optional
- A field that may be absent, written with a question mark.
- contract
- The shape callers are required to honor.
Quiz
Type the string. Keep the understanding.
The terminal cannot execute this language. It only prints ACCEPTED or REJECTED when your string matches the lesson.