Combining Type Aliases
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 35 of 73.
One of the most powerful aspects of type aliases is their ability to be combined and reused to create more sophisticated type definitions. Instead of defining complex types from scratch each time, you can build them by composing smaller, focused type aliases together.
This approach promotes code reusability and makes your type definitions more maintainable. When you need to update a base type, all the composed types that use it automatically inherit the changes:
type Email = string;
type Phone = number;
type Contact = {
name: string;
email: Email;
phone: Phone;
};You can also combine type aliases within union types to create flexible data structures. This is particularly useful when an object property might accept different formats:
type ContactMethod = Email | Phone;
type User = {
id: number;
preferredContact: ContactMethod;
};This compositional approach makes your code more expressive and easier to understand. Rather than seeing generic types like string or number, other developers immediately understand the intended purpose through meaningful type names like Email and Phone.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a type alias named Username for the string type.
Create a type alias named UserAge for the number type.
Create a type alias named ContactMethod that combines Username and UserAge using a union type.
Create a type alias named UserProfile for an object with the following properties:
idof typenumberdisplayNameof typeUsernamepreferredContactof typeContactMethod
Create a type alias named AdminProfile that combines UserProfile with an additional object type containing a permissions property of type string using intersection types.
Now create the following variables:
- Create a variable named
regularUserof typeUserProfilewith id1, displayName"john_doe", and preferredContact"john_doe" - Create a variable named
systemAdminof typeAdminProfilewith id2, displayName"admin", preferredContact25, and permissions"full_access"
Create a function named getContactInfo that accepts a parameter called contact of type ContactMethod and returns a string. Use a type guard to check the type and return:
- If it's a string:
"Contact: [contact]" - If it's a number:
"Age: [contact]"
Print the following outputs on separate lines:
- Call
getContactInfowithregularUser.preferredContactand print the result - Call
getContactInfowithsystemAdmin.preferredContactand print the result - Print
systemAdmin.permissions
Cheat sheet
Type aliases can be combined and reused to create more sophisticated type definitions. This promotes code reusability and maintainability:
type Email = string;
type Phone = number;
type Contact = {
name: string;
email: Email;
phone: Phone;
};Combine type aliases within union types to create flexible data structures:
type ContactMethod = Email | Phone;
type User = {
id: number;
preferredContact: ContactMethod;
};This compositional approach makes code more expressive and easier to understand by using meaningful type names instead of generic types like string or number.
Try it yourself
// TODO: Write your code here
// Create type aliases for Username, UserAge, ContactMethod, UserProfile, and AdminProfile
// Create variables regularUser and systemAdmin
// Create getContactInfo function
// Print the required outputsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples