0
0
NestJSframework~30 mins

TypeScript-first philosophy in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Building a Simple NestJS Service with TypeScript-First Philosophy
📖 Scenario: You are creating a small backend service using NestJS. NestJS encourages a TypeScript-first approach, meaning you write your data types and logic in TypeScript first, which helps catch errors early and makes your code easier to understand.In this project, you will create a simple service that manages a list of books with their titles and authors.
🎯 Goal: Build a NestJS service that stores a list of books using TypeScript interfaces and classes. You will define the data structure, configure a service variable, implement a method to retrieve books, and complete the service class.
📋 What You'll Learn
Create a TypeScript interface for a Book with title and author as strings
Create a service class called BooksService
Add a private array variable books of type Book[] with initial data
Implement a method getAllBooks() that returns the array of books
Complete the service class with the correct export statement
💡 Why This Matters
🌍 Real World
Backend services often manage data with clear types to avoid bugs. NestJS uses TypeScript-first philosophy to help developers write safer and clearer code.
💼 Career
Understanding how to define data types and services in NestJS is essential for backend development roles using modern TypeScript frameworks.
Progress0 / 4 steps
1
Define the Book interface
Create a TypeScript interface called Book with two string properties: title and author.
NestJS
Need a hint?

Use the interface keyword to define the shape of a book object.

2
Create the BooksService class with initial data
Create a class called BooksService. Inside it, add a private variable called books of type Book[] and initialize it with two books: { title: 'The Hobbit', author: 'J.R.R. Tolkien' } and { title: '1984', author: 'George Orwell' }.
NestJS
Need a hint?

Use private books: Book[] = [...] inside the class to store the books.

3
Add a method to get all books
Inside the BooksService class, add a public method called getAllBooks() that returns the books array.
NestJS
Need a hint?

Define a method that returns the private books array.

4
Export the BooksService class
Make sure the BooksService class is exported so it can be used in other parts of the application.
NestJS
Need a hint?

Use the export keyword before the class declaration.