Type aliases let you give a new name to a type. This makes your code easier to read and reuse.
Creating type aliases in Typescript
type NewName = ExistingType;
The keyword type starts the alias.
You can use any valid type after the equals sign, like primitives, objects, unions, or intersections.
Name that is the same as string.type Name = string;
ID can be a number or a string.type ID = number | string;
Point describes an object with x and y as numbers.type Point = { x: number; y: number };Callback is a function type that takes a string and returns nothing.type Callback = (data: string) => void;This program creates type aliases for UserID and User. It uses them to type function parameters and variables. It then prints greetings for two users.
type UserID = number | string;
type User = {
id: UserID;
name: string;
};
function greet(user: User): string {
return `Hello, ${user.name}! Your ID is ${user.id}.`;
}
const user1: User = { id: 123, name: "Alice" };
const user2: User = { id: "abc-456", name: "Bob" };
console.log(greet(user1));
console.log(greet(user2));Type aliases do not create new types, just new names for existing types.
You cannot use type aliases to create values, only types.
Use type aliases to make your code easier to understand and maintain.
Type aliases give a new name to a type for easier reuse.
They can represent simple or complex types like unions and objects.
Use them to make your code clearer and less repetitive.
Practice
Solution
Step 1: Understand what a type alias does
A type alias creates a new name for an existing type, making code easier to read and reuse.Step 2: Compare options with the definition
Only To give a new name to a type for easier reuse correctly describes this purpose; others describe unrelated concepts.Final Answer:
To give a new name to a type for easier reuse -> Option DQuick Check:
Type alias = new name for type [OK]
- Confusing type alias with variable declaration
- Thinking type alias creates new data
- Mixing type alias with function definitions
Age for the type number?Solution
Step 1: Recall TypeScript syntax for type aliases
The correct syntax uses the keywordtype, followed by the alias name, an equals sign, and the type.Step 2: Check each option
type Age = number; matches the correct syntax exactly. Options B, C, and D have syntax errors or wrong keywords.Final Answer:
type Age = number; -> Option BQuick Check:
type alias syntax = type Name = Type [OK]
- Using 'alias' instead of 'type'
- Using '==' instead of '='
- Confusing type alias with variable declaration
type ID = string | number;
function printId(id: ID) {
console.log(typeof id);
}
printId(123);
printId('abc');Solution
Step 1: Understand the type alias and function behavior
The aliasIDallowsstringornumber. The function prints the type of the argument.Step 2: Trace the function calls
CallingprintId(123)prints 'number'. CallingprintId('abc')prints 'string'.Final Answer:
number string -> Option CQuick Check:
typeof 123 = number, typeof 'abc' = string [OK]
- Assuming type alias changes runtime behavior
- Expecting compile error for union types
- Mixing order of printed types
type User = {
name: string;
age: number;
}
const user: User = {
name: 'Alice',
age: '25'
};Solution
Step 1: Check the type alias definition
The aliasUserexpectsageto be a number.Step 2: Check the object assigned to
The object hasuserage: '25', which is a string, causing a type error.Final Answer:
Property 'age' should be a number, not a string -> Option AQuick Check:
Type mismatch in object property [OK]
- Ignoring type mismatch in object properties
- Thinking semicolon is mandatory after type alias
- Confusing variable declaration rules with type alias
type Point = { x: number; y: number };
type Circle = { center: Point; radius: number };
type Shape = Circle | Point;Which of the following is a valid
Shape object?Solution
Step 1: Understand the type aliases
Pointis an object withxandyas numbers.Circlehas acenterof typePointand aradiusnumber.Shapecan be either.Step 2: Check each option against the types
{ center: { x: 0, y: 0 }, radius: 5 } matchesCircleexactly. { x: 0, y: 0, radius: 5 } has extraradiuson aPointshape, invalid. { center: { x: 0 }, radius: 5 } has incompletecenter. { x: '0', y: 0 } hasxas string, invalid.Final Answer:
{ center: { x: 0, y: 0 }, radius: 5 } -> Option AQuick Check:
Valid Shape = Circle or Point matching types [OK]
- Mixing properties of union types incorrectly
- Using wrong property types inside objects
- Assuming partial objects are valid
Ready to prove it?
Pass the test and this pattern moves unknown
