TypeScript Tutorial
TypeScript Type Aliases
type gives a name to a shape. Use it so you do not repeat the same object type in every function.
Another name for a type
In C++, a reference is another name for a variable. In TypeScript, a type alias is another name for atype. type Point = { x: number; y: number } does not create a point. It names the shape so you can write Point instead of repeating the braces.
The alias and the original type are the same to the checker. There is no extra object at runtime. The wordPoint is for you and for tsc.
Name an object type
Write type, the name, equals, then the shape. Use that name on variables. The values still look like the objects from the last chapter.
Example
type Point = { x: number; y: number };
const p: Point = { x: 3.0, y: 4.0 };
console.log(p.x + ", " + p.y);Change the fields in /typescript/try. If you omit y, tsc reports that Point is missing a property. The alias is what makes that message readable.
Reuse the name
Two functions that both take { name: string; score: number } are hard to keep in sync. Give the record a name. Every parameter and return type that needs that shape uses Player.
Example
type Player = { name: string; score: number };
function describe(p: Player): string {
return p.name + " has " + p.score;
}
function bonus(p: Player, extra: number): Player {
return { name: p.name, score: p.score + extra };
}
const hero: Player = { name: "Nia", score: 1200 };
const after: Player = bonus(hero, 50);
console.log(describe(hero));
console.log(describe(after));bonus returns a new Player. It does not rename hero. The alias is the type; hero and after are two values of that type.
Aliases for primitives too
You can alias number or string when the name documents a unit: a user id, a temperature. The checker still treats UserId as number. The name is for readers.
Example
type UserId = number;
type Label = string;
const id: UserId = 42;
const label: Label = "guest";
console.log(label + " #" + id);That is not a new kind of number. It is a nickname. For a closed set of named constants, use an enum. For a shape you will repeat, use type.
Alias versus variable
| C++ reference | TypeScript type alias | |
|---|---|---|
| What it names | An existing variable | An existing type |
| At runtime | Same object, two names | Nothing extra — types erase |
| Write | int& r = x; | type Point = { x: number } |
Do not expect type to alias a value. const r = x is still how you bind another variable. For objects that copies the reference, as the objects chapter showed. For types, typeis the tool.
type versus interface
TypeScript also has interface, which can name an object shape. This course uses typefor aliases in this chapter. Interfaces return in a later lesson. Either can describe a point. The keywordtype also covers unions, which interface does not.
A type alias cannot be assigned a new shape later in the file. You name it once. If you need a value that might be a string or might be missing, that is a union, not a second alias.
Next: a union is one of several types — TypeScript’s analog of a pointer that might be null.