
Type Aliases in TypeScript: Creating Reusable Types
Simplify complex types with meaningful names
Catalog
Click on this table of contents to jump to the corresponding section
In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't very practical as it's easy using primitive types, however, it can be used for documentation purposes. Type aliasing is just giving another name for a type. let's demonstrate a few examples on type Aliasing.
Example 1: Creating a type alias
In this example we create a type alias named type_alias. The new type is a union of number, string, and boolean. if a new variable is declared with that type, the new variable can be given values that are only of types number, string, or boolean.
Output:
Here we try to give a variable of type function, typescript compiler raises an error.
Output:
Example 2: Implementing type alias in a function.
In this example, we will try implementing type alias in a function. We create a type alias called anotherType which is a union of number and string type. The function DisplayId can take arguments that are of type number or string. If any other type is given error is raised.
Output:
Example 3: String literals as type alias.
Instead of specifying types in union, we can use strings instead. A new type is created which is type alias of "yes"|"no". only "yes" or "no " strings can be given as values for the variables declared with otherType. when some other value is given as input typescript compiler raises error.
Output:
typescript compiler raises error:
After executing JavaScript file:
Lily and 4 people like this
Prev Post
Space The Final Frontier
Next Post
Telescopes 101
0 Comments
Leave a Reply
Recent Post

SMOTE for Imbalanced Classification with Python
12:28:00 16/05/2026

CLAHE Histogram Equalization with OpenCV: A Python Guide
12:06:00 16/05/2026

Histogram Equalization in Digital Image Processing
10:56:00 16/05/2026

Gaussian Noise Explained: What It Is and How It Works
10:36:00 16/05/2026

Python Image Blurring with OpenCV: Gaussian, Median & Bilateral Filter Guide
09:59:00 16/05/2026
Related Topics
Feeds
Don't miss what's next 👋
Enjoyed this content? Leave your email to get notified when we publish new insights, tutorials, and updates — no spam, ever.

