Ad space (banner)
Lesson 9 / 20

Type Aliases (type)

This lesson covers type (a type alias), which lets you give a type a different name. It's for anyone searching "TypeScript type alias."

Writing type Name = type; attaches a short name to a complex type. This cuts down on repeating the same type over and over, and makes your code's intent easier to convey.

The example defines a type alias, type Point = { x: number; y: number };, and uses it for objects representing two coordinates.

A common early mistake is being unsure whether to use interface or type. For simply defining an object's shape, there's not much difference between the two, but for expressing "one of several possible types" โ€” like a union type, covered in the next lesson โ€” only type will work.

In real projects, teams and projects often settle on their own rule for when to use interface versus type.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run TypeScript directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)