0
0
Typescriptprogramming~5 mins

Creating type aliases in Typescript - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What is a type alias in TypeScript?
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
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
Can a type alias be used to name a tuple type?
AOnly with classes
BYes
COnly with interfaces
DNo
Which of these can type aliases NOT do?
AName object types
BRepresent union types
CName primitive types
DExtend other types by reopening
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; };
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.