0
0
Typescriptprogramming~10 mins

Type alias vs inline types in Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a type alias for a user object.

Typescript
type User = [1];
Drag options to blanks, or click blank then click option'
A[string, number]
B{ name: string; age: number }
Cstring | number
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using array syntax instead of object syntax.
Using union types instead of object types.
2fill in blank
medium

Complete the function parameter type using an inline type instead of a type alias.

Typescript
function greet(user: [1]) { console.log(`Hello, ${user.name}`); }
Drag options to blanks, or click blank then click option'
Anumber
BUser
Cstring
D{ name: string; age: number }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type alias name instead of inline type.
Using primitive types like string or number instead of object type.
3fill in blank
hard

Fix the error in the type alias declaration for a product with id and name.

Typescript
type Product = [1];
Drag options to blanks, or click blank then click option'
A{ id: number; name: string }
B(id: number, name: string) => void
C[number, string]
Dnumber | string
Attempts:
3 left
💡 Hint
Common Mistakes
Using function type syntax instead of object type.
Using tuple or union types instead of object type.
4fill in blank
hard

Fill both blanks to create a type alias and use it in a function parameter.

Typescript
type [1] = { title: string; pages: number };
function printBook(book: [2]) { console.log(book.title); }
Drag options to blanks, or click blank then click option'
ABook
BArticle
DMagazine
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the type alias and parameter type.
Using inline type in one place and alias in another.
5fill in blank
hard

Fill all three blanks to define a type alias, use it inline, and declare a variable with that type.

Typescript
type [1] = { x: number; y: number };
const point: [2] = { x: 10, y: 20 };
function logPoint(p: [3]) { console.log(`x: ${p.x}, y: ${p.y}`); }
Drag options to blanks, or click blank then click option'
APoint
DCoordinate
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the type alias in different places.
Mixing inline types and type aliases inconsistently.