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++ referenceTypeScript type alias
What it namesAn existing variableAn existing type
At runtimeSame object, two namesNothing extra — types erase
Writeint& 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.

FAQ: TypeScript Type Aliases

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript type aliases examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript type aliases in this TypeScript TypeScript lesson (TypeScript Type Aliases).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.