define a map of books and isbn numbers in typescript

In TypeScript, you can define a map of books and ISBN numbers using an object literal with key-value pairs. The keys are book titles, and the values are the ISBN numbers. Here's an example:

index.ts
const booksMap: {[title: string]: string} = {
  "The Great Gatsby": "978-0743273565",
  "To Kill a Mockingbird": "978-0446310789",
  "1984": "978-0451524935",
};
162 chars
6 lines

This creates a booksMap constant that maps book titles (strings) to ISBN numbers (also strings). You can add more books and ISBN numbers to the map as needed.

You can also define the map using the Map class in TypeScript, as shown below:

index.ts
const booksMap = new Map<string, string>([
  ["The Great Gatsby", "978-0743273565"],
  ["To Kill a Mockingbird", "978-0446310789"],
  ["1984", "978-0451524935"],
]);
166 chars
6 lines

This creates a booksMap constant that maps book titles (keys) to ISBN numbers (values) using the Map constructor. The types of the keys and values are defined explicitly as string.

Both approaches will allow you to store and retrieve books and ISBN numbers efficiently in TypeScript.

gistlibby LogSnag