0
0
Typescriptprogramming~5 mins

Type alias vs inline types in Typescript - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is a type alias in TypeScript?
A type alias is a custom name you give to a type or a combination of types. It helps you reuse and simplify complex types by giving them a simple name.
Click to reveal answer
beginner
What does inline type mean in TypeScript?
An inline type is a type written directly where it is used, without giving it a separate name. It is often used for quick, one-time type definitions.
Click to reveal answer
beginner
Give an example of a type alias for an object with properties name (string) and age (number).
type Person = { name: string; age: number; };
Click to reveal answer
beginner
How would you write the same object type { name: string; age: number; } as an inline type in a function parameter?
function greet(person: { name: string; age: number }) { /* ... */ }
Click to reveal answer
intermediate
What is one advantage of using type aliases over inline types?
Type aliases make code easier to read and reuse because you give complex types a simple name and can use them multiple times without repeating the full type.
Click to reveal answer
Which of the following is a correct type alias declaration in TypeScript?
Atype User = { id: number; name: string; };
Balias User = { id: number; name: string; };
Ctypedef User = { id: number; name: string; };
Ddefine User = { id: number; name: string; };
What is an inline type in TypeScript?
AA type defined directly where it is used without a name.
BA type alias with a custom name.
CA type imported from another file.
DA type that can only be used once.
Why might you prefer a type alias over inline types?
ATo make the code run faster.
BBecause inline types are not allowed in TypeScript.
CTo avoid using interfaces.
DTo reuse the type and improve code readability.
Which of these is an example of an inline type used in a function parameter?
Atype User = { id: number; name: string };
Bfunction printUser(user: { id: number; name: string }) {}
Cinterface User { id: number; name: string }
Dconst user: User = { id: 1, name: 'Alice' };
Which keyword is used to create a type alias in TypeScript?
Ainterface
Balias
Ctype
Dtypedef
Explain the difference between a type alias and an inline type in TypeScript.
Think about naming and reuse.
You got /4 concepts.
    When would you choose to use a type alias instead of an inline type?
    Consider code reuse and clarity.
    You got /4 concepts.