Course repository: https://github.com/MoonHighway/applied-typescript
Required: Node.js >= v18.0.0
Recommended: Visual Studio Code (with GitHub Copilot disabled)
Setup
If you only have a README in your cloned copy of the repository, run:
git pull
Install dependencies by running:
npm install
keyof
type operator, Mapped types, Template literal typessatisfies
operatorIntersection types
Extending interfaces
Union types
Indexed access types
{ name: string; } & { age: number; }
interface User {
name: string;
}
interface SuperAdmin extends User {
enabled: boolean;
}
string | number | boolean
null | { value: number; }
type User = {
name: string;
age: number;
}
type Age = User["age"];
// type Age = string;
type Country<LanguagesType> = {
name: string;
languages: LanguagesType;
};
Type variable
Type parameter
type BoxIsShape = Box extends Shape ? boolean : unknown;
Conditional type constraint
True
branch
Box
is assignable
to Shape
False
branch
keyof
type operatorProduce a string or numeric literal union of the keys of an object type
interface User {
name: string;
age: number;
}
type UserKeys = keyof User;
// type UserKeys = "name" | "age";
Iterate through properties of an object type to create a new object type
type CountryData = {
name: string;
languages: string[];
population: number;
};
type Descriptions<Type> = {
[Key in keyof Type]: string;
};
Enforce specific string formats or expand string literal unions
type Greeting = `Welcome ${string}!`;
const greeting: Greeting = "Welcome home!";
Same syntax as template
literal strings in JavaScript
if (typeof population === "number") {
if (location instanceof City) {
Automatically recognised by TypeScript and used to narrow the type of a value
function valueIsString(value: any): value is string {
function assertIsString(
value: unknown
): asserts value is string {
type Product = {
name: string;
price: number;
};
type Service = {
name: string;
price: number;
};
type CatalogItem = Product | Service;
const catalog: CatalogItem[] = [...];
discriminate /dĭ-skrĭm′ə-nāt″/
intransitive verb
1. To make a clear distinction; distinguish.
"discriminate among the options available."
2. To perceive or notice the distinguishing features of; recognize as distinct.
"unable to discriminate colors."
Source: The American Heritage® Dictionary of the English Language, 5th Edition
type Product = {
type: "product";
name: string;
price: number;
};
type Service = {
type: "service";
name: string;
price: number;
};
type CatalogItem = Product | Service;
const catalog: CatalogItem[] = [...];
Discriminant property
Discriminant property
declare const __brand: unique symbol;
type Brand<BaseType, BrandedName extends string> = BaseType & {
[__brand]: BrandedName;
};
// ----
type Cat = {
name: string;
color: string;
};
type BrandedCat = Brand<Cat, "Cat">;
Emulate nominal types by creating structurally unique types
A structurally unique type
TypeScript magic ✨
satisfies
operatorconst configA = {
logging: true,
metricsKey: "app_metrics"
};
const configB: Record<string, boolean | string> = {
logging: true,
metricsKey: "app_metrics"
};
const configC = {
logging: true,
metricsKey: "app_metrics"
} satisfies Record<string, boolean | string>;
Ensure that an expression matches a type, but retain the specific inferred type