Type Aliases for Primitives
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 30 of 73.
TypeScript's type keyword allows you to create a type alias - essentially a custom name for any existing type, including primitive types and their combinations.
The syntax is straightforward: you use the type keyword followed by your chosen name, an equals sign, and the type you want to alias:
type UserID = string | number;
type Score = number;
type IsActive = boolean;Once you've created a type alias, you can use it anywhere you would use the original type. This is particularly valuable when working with union types, as it makes your code more readable and self-documenting:
type UserID = string | number;
let currentUser: UserID = "user123";
let adminUser: UserID = 42;Type aliases don't create new types - they simply provide alternative names for existing ones. This means UserID and string | number are completely interchangeable, but using the alias makes your code's intent much clearer to other developers.
Challenge
EasyCreate a type alias named UserID for a union type that can be either a string or a number.
Create a type alias named Priority for a union type that can be either a string or a boolean.
Create a type alias named Status for the string type.
Now declare the following variables using your type aliases:
- Declare a variable named
currentUserof typeUserIDand assign it the string value"admin123" - Declare a variable named
guestUserof typeUserIDand assign it the number value42 - Declare a variable named
taskPriorityof typePriorityand assign it the string value"high" - Declare a variable named
isUrgentof typePriorityand assign it the boolean valuetrue - Declare a variable named
orderStatusof typeStatusand assign it the string value"pending"
Print each variable's value on a separate line in the order they were declared above.
Try it yourself
// TODO: Write your code here
// Create type aliases for UserID, Priority, and Status
// Then declare the variables and assign the specified values
// Print each variable's valueThis 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