A type alias is a way to give a new name to a type. It helps make code easier to read and reuse by creating a simple name for complex types.
Click to reveal answer
beginner
How do you create a type alias for a string in TypeScript?
You use the type keyword followed by the new name and the type. For example:
type Name = string;
Click to reveal answer
intermediate
Can type aliases represent union types? Give an example.
Yes, type aliases can represent union types. For example:
type Status = "success" | "error" | "loading";
This means Status can be one of those three string values.
Click to reveal answer
intermediate
What is the difference between a type alias and an interface in TypeScript?
Type aliases can name any type, including primitives, unions, and tuples. Interfaces describe object shapes and can be extended. Type aliases cannot be reopened to add new properties, but interfaces can.
Click to reveal answer
beginner
Show how to create a type alias for an object with properties id (number) and name (string).
You write:
type User = { id: number; name: string; };
This creates a new type called User representing that object shape.
Click to reveal answer
Which keyword is used to create a type alias in TypeScript?
Atype
Balias
Cinterface
Dclass
✗ Incorrect
The type keyword is used to create type aliases.
What does this type alias represent?
type ID = number | string;
AID can only be a number
BID is a function
CID can be a number or a string
DID is an object
✗ Incorrect
The union number | string means ID can be either a number or a string.
Can a type alias be used to name a tuple type?
AOnly with classes
BYes
COnly with interfaces
DNo
✗ Incorrect
Type aliases can name tuple types, for example: type Point = [number, number];
Which of these can type aliases NOT do?
AName object types
BRepresent union types
CName primitive types
DExtend other types by reopening
✗ Incorrect
Type aliases cannot be reopened to add new properties or extend like interfaces can.
What is the correct syntax to create a type alias for an object with a string property 'title'?
Atype Book = { title: string; };
Binterface Book = { title: string; };
Cclass Book { title: string; }
Dalias Book = { title: string; };
✗ Incorrect
The correct syntax uses type followed by the name and the object shape.
Explain what a type alias is and how it helps in TypeScript programming.
Think about giving a nickname to a type.
You got /4 concepts.
Describe the difference between a type alias and an interface in TypeScript.
Focus on their capabilities and usage.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a type alias in TypeScript?
easy
A. To import external modules
B. To create a new variable
C. To define a function
D. To give a new name to a type for easier reuse
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 D
Quick Check:
Type alias = new name for type [OK]
Hint: Type aliases rename types to simplify code [OK]
Common Mistakes:
Confusing type alias with variable declaration
Thinking type alias creates new data
Mixing type alias with function definitions
2. Which of the following is the correct syntax to create a type alias named Age for the type number?
easy
A. alias Age: number;
B. type Age = number;
C. let Age: number;
D. type Age == number;
Solution
Step 1: Recall TypeScript syntax for type aliases
The correct syntax uses the keyword type, 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 B
Quick Check:
type alias syntax = type Name = Type [OK]
Hint: Use 'type Name = Type;' to create aliases [OK]
Common Mistakes:
Using 'alias' instead of 'type'
Using '==' instead of '='
Confusing type alias with variable declaration
3. What will be the output of this TypeScript code?
type ID = string | number;
function printId(id: ID) {
console.log(typeof id);
}
printId(123);
printId('abc');
medium
A. Error at compile time
B. string
number
C. number
string
D. ID
ID
Solution
Step 1: Understand the type alias and function behavior
The alias ID allows string or number. The function prints the type of the argument.
Step 2: Trace the function calls
Calling printId(123) prints 'number'. Calling printId('abc') prints 'string'.
Final Answer:
number
string -> Option C
Quick Check:
typeof 123 = number, typeof 'abc' = string [OK]
Hint: Check typeof values for union type inputs [OK]
Common Mistakes:
Assuming type alias changes runtime behavior
Expecting compile error for union types
Mixing order of printed types
4. Identify the error in this TypeScript code:
type User = {
name: string;
age: number;
}
const user: User = {
name: 'Alice',
age: '25'
};
medium
A. Property 'age' should be a number, not a string
B. Missing semicolon after type alias
C. Type alias name cannot be 'User'
D. Object properties must be declared with 'let'
Solution
Step 1: Check the type alias definition
The alias User expects age to be a number.
Step 2: Check the object assigned to user
The object has age: '25', which is a string, causing a type error.
Final Answer:
Property 'age' should be a number, not a string -> Option A
Quick Check:
Type mismatch in object property [OK]
Hint: Match property types exactly in objects [OK]
Common Mistakes:
Ignoring type mismatch in object properties
Thinking semicolon is mandatory after type alias
Confusing variable declaration rules with type alias
5. Given these type aliases:
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?
hard
A. { center: { x: 0, y: 0 }, radius: 5 }
B. { x: 0, y: 0, radius: 5 }
C. { center: { x: 0 }, radius: 5 }
D. { x: '0', y: 0 }
Solution
Step 1: Understand the type aliases
Point is an object with x and y as numbers. Circle has a center of type Point and a radius number. Shape can be either.
Step 2: Check each option against the types
{ center: { x: 0, y: 0 }, radius: 5 } matches Circle exactly. { x: 0, y: 0, radius: 5 } has extra radius on a Point shape, invalid. { center: { x: 0 }, radius: 5 } has incomplete center. { x: '0', y: 0 } has x as string, invalid.
Final Answer:
{ center: { x: 0, y: 0 }, radius: 5 } -> Option A
Quick Check:
Valid Shape = Circle or Point matching types [OK]
Hint: Match object shape exactly for union types [OK]
Common Mistakes:
Mixing properties of union types incorrectly
Using wrong property types inside objects
Assuming partial objects are valid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.