Sub topic 3
Naming Types with Type Aliases
Assume you know basic TypeScript annotations and can run the TypeScript compiler. This chapter focuses on type aliases: names that refer to existing types or combinations of types.
What a type alias is
A type alias gives a type another name. It does not create a new runtime value or a distinct runtime type.
type UserId = number;
type UserName = string;
const id: UserId = 42;
const name: UserName = "Ada";The compiler treats UserId as number and UserName as string. Aliases can make intent clearer, especially when a type expression is long or appears in several places.
A type alias can describe an object shape:
type User = {
id: number;
name: string;
email?: string;
};
const user: User = {
id: 1,
name: "Ada",
};The object is checked by its shape. A value does not need to be created specifically as a User for it to match the alias.
Aliases for unions and literal types
A major difference between aliases and interfaces is that an alias can directly name a union. A union means that a value can have one of several types.
type Status = "idle" | "loading" | "done";
let status: Status = "loading";
// status = "paused"; // errorThe members here are string literal types. The variable accepts only the listed strings.
Aliases also work well for discriminated unions. A discriminant is a property whose value tells TypeScript which member it has.
type Result =
| { ok: true; value: string }
| { ok: false; error: Error };
function describe(result: Result): string {
return result.ok ? result.value : result.error.message;
}
console.log(describe({ ok: true, value: "Saved" }));When result.ok is true, TypeScript knows that value exists. When it is false, TypeScript knows that error exists.
Combining and parameterizing aliases
An intersection combines types. The resulting value must satisfy every member.
type Named = { name: string };
type Identified = { id: number };
type RecordInfo = Named & Identified;
const item: RecordInfo = {
name: "Report",
id: 7,
};Aliases can also describe tuples, function types, and generic shapes.
type Pair<A, B> = [A, B];
type Formatter = (input: string) => string;
const entry: Pair<string, number> = ["age", 30];
const upper: Formatter = (value) => value.toUpperCase();A generic alias uses type parameters as placeholders. Each use supplies concrete types.
Limits and practical choices
A type alias can express unions, intersections, tuples, primitive aliases, function types, and mapped types. An interface is often preferable for a public object contract that may be extended or declaration-merged. Both can describe ordinary object shapes, so use your team's convention consistently.
A type alias cannot be redeclared with the same name:
type Settings = { host: string };
// type Settings = { port: number }; // Duplicate identifierIf you need to build an alias from another object type, use an intersection:
type BaseConfig = { host: string };
type Config = BaseConfig & { port: number };Exercise
Which alias describes a value that is either a success containing text or a failure containing an Error?
Show answer
type Result = { ok: true; value: string } | { ok: false; error: Error };