Type Aliases for Objects
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 38 of 73.
TypeScript allows you to create a type alias that defines a reusable shape for objects.
A type alias uses the type keyword to create a named definition for an object structure. Once defined, you can use this alias anywhere you need that specific object shape:
type Product = {
name: string;
price: number;
inStock: boolean;
};
let laptop: Product = {
name: "Gaming Laptop",
price: 1299,
inStock: true
};
let phone: Product = {
name: "Smartphone",
price: 699,
inStock: false
};Challenge
EasyCreate a type alias named Book for an object with the following properties:
titleof typestringauthorof typestringpagesof typenumberisAvailableof typeboolean
Create a type alias named Movie for an object with the following properties:
titleof typestringdirectorof typestringdurationof typenumberratingof typestring
Using your type aliases, create the following variables:
- Create a variable named
novelof typeBookwith title"The Great Gatsby", author"F. Scott Fitzgerald", pages180, and isAvailabletrue - Create a variable named
textbookof typeBookwith title"TypeScript Handbook", author"Microsoft", pages450, and isAvailablefalse - Create a variable named
filmof typeMoviewith title"Inception", director"Christopher Nolan", duration148, and rating"PG-13"
Create a function named getBookInfo that accepts a parameter of type Book and returns a string in the format "[title] by [author] - [pages] pages".
Create a function named getMovieInfo that accepts a parameter of type Movie and returns a string in the format "[title] directed by [director] ([duration] min)".
Print the following outputs on separate lines:
- Call
getBookInfowithnoveland print the result - Call
getBookInfowithtextbookand print the result - Call
getMovieInfowithfilmand print the result - Print the availability status of
novel(theisAvailableproperty)
Try it yourself
// TODO: Write your code here
// Create type aliases for Book and Movie
// Create variables using the type aliases
// Create functions to get information
// Print the required outputsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples6Typing Objects and Interfaces
Inline Object Type AnnotationsType Aliases for ObjectsIntroduction to InterfacesInterfaces vs. Type AliasesOptional & Readonly PropsExtending Interfaces and TypesAdding Methods to InterfacesRecap: Defining Object Shapes