0
0
Typescriptprogramming~30 mins

Explicit type annotations in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Explicit type annotations
📖 Scenario: You are creating a simple program to store and display information about a book in a library system.
🎯 Goal: Learn how to use explicit type annotations in TypeScript to define the types of variables and function parameters.
📋 What You'll Learn
Create variables with explicit type annotations
Define a function with typed parameters and return type
Use the variables and function to display information
💡 Why This Matters
🌍 Real World
Explicit type annotations help developers understand what kind of data variables and functions expect, reducing bugs in real-world applications like library systems.
💼 Career
Many programming jobs require writing clear, typed code to maintain large codebases and collaborate with teams effectively.
Progress0 / 4 steps
1
Create variables with explicit type annotations
Create a variable called title with type string and value "The Great Gatsby". Create a variable called year with type number and value 1925.
Typescript
Need a hint?

Use let variableName: type = value; to declare variables with explicit types.

2
Add a variable with explicit boolean type
Create a variable called available with type boolean and value true.
Typescript
Need a hint?

Boolean variables can be declared with type boolean and values true or false.

3
Create a function with typed parameters and return type
Create a function called getBookInfo that takes parameters title (type string), year (type number), and available (type boolean). The function should return a string combining these values in the format: "Title (Year) - Available" or "Title (Year) - Not Available" depending on the available value.
Typescript
Need a hint?

Use function functionName(param: type, ...): returnType { ... } to define typed functions.

4
Display the book information using the function
Use console.log to print the result of calling getBookInfo with the variables title, year, and available.
Typescript
Need a hint?

Call the function with the variables and print the result using console.log.