Przeglądaj źródła

6.6: defining and using interfaces.

Frederic G. MARAND 5 lat temu
rodzic
commit
1b436b2950
3 zmienionych plików z 79 dodań i 103 usunięć
  1. 52 103
      app/app.ts
  2. 11 0
      app/enums.ts
  3. 16 0
      app/interfaces.ts

+ 52 - 103
app/app.ts

@@ -1,44 +1,37 @@
-enum Category {
-  Biography,     // 0
-  Poetry,        // 1
-  Fiction,       // 2
-}
-
-let books: ({ author: string; available: boolean; category: Category; id: number; title: string })[] = [];
-
-function GetAllBooks() {
-  if (books.length < 4) {
-    books = [
-      {
-        author: 'James Joyce',
-        available: true,
-        category: Category.Fiction,
-        id: 1,
-        title: 'Ulysses',
-      },
-      {
-        author: 'Ernest Hemingway',
-        available: false,
-        category: Category.Fiction,
-        id: 2,
-        title: 'A farewall to arms',
-      },
-      {
-        author: 'Maya Angelou',
-        available: true,
-        category: Category.Poetry,
-        id: 3,
-        title: 'I know why the caged bird sings',
-      },
-      {
-        author: 'Herman Melville',
-        available: true,
-        category: Category.Fiction,
-        id: 4,
-        title: 'Moby Dick',
-      }
-    ];
-  }
+import {Book} from './interfaces';
+import {Category} from "./enums";
+
+function GetAllBooks(): Book[] {
+  const books = [
+    {
+      author: 'James Joyce',
+      available: true,
+      category: Category.Fiction,
+      id: 1,
+      title: 'Ulysses',
+    },
+    {
+      author: 'Ernest Hemingway',
+      available: false,
+      category: Category.Fiction,
+      id: 2,
+      title: 'A farewall to arms',
+    },
+    {
+      author: 'Maya Angelou',
+      available: true,
+      category: Category.Poetry,
+      id: 3,
+      title: 'I know why the caged bird sings',
+    },
+    {
+      author: 'Herman Melville',
+      available: true,
+      category: Category.Fiction,
+      id: 4,
+      title: 'Moby Dick',
+    }
+  ];
 
   return books;
 }
@@ -100,7 +93,7 @@ function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): A
   return filteredTitles;
 }
 
-function GetBookById(id: number) {
+function GetBookById(id: number): Book {
   const allBooks = GetAllBooks();
   return allBooks.filter(book => book.id === id)[0];
 }
@@ -142,67 +135,23 @@ function GetTitles(bookProperty: any): string[] {
   return foundTitles;
 }
 
-//******************************************************************************
-function defaultParamsDemo() {
-  CreateCustomer('Michelle');
-  CreateCustomer('Leigh', 6);
-  CreateCustomer('Marie', 12, 'Atlanta');
-}
-
-function functionTypeDemos() {
-  let idGenerator: (chars: string, nums: number) => string;
-  idGenerator = CreateCustomerId;
-
-  let myID: string = idGenerator('Daniel', 10);
-  console.log(myID);
-
-  idGenerator = (name, id) => id + name;
-  myID = idGenerator('Daniel', 10);
-  console.log(myID);
-}
-
-// const fictionBookTitles = GetBookTitlesByCategory(Category.Fiction);
-// fictionBookTitles.forEach((val, idx, arr) => console.log(`${++idx} ${val}.`));
-// let poetryBookTitles = GetBookTitlesByCategory();
-// console.log(poetryBookTitles);
-
-function functionCallDefaultParamsDemo() {
-  console.log("Book count:", books.length);
-  books.unshift({
-    author: "Anon1",
-    available: true,
-    category: Category.Biography,
-    id: 42,
-    title: "Added1",
-  });
-  console.log("Book count:", books.length);
-  LogFirstAvailable();
-  books.unshift({
-    author: "Anon2",
-    available: true,
-    category: Category.Biography,
-    id: 42,
-    title: "Added2",
-  });
-  console.log("Book count:", books.length);
-  // Default is now 5: it is evaluated on each call.
-  LogFirstAvailable();
+function PrintBook(book: Book): void {
+  console.log(`${book.title} by ${book.author}.`);
 }
 
-function restParamsDemo() {
-  let myBooks = CheckoutBooks('Ripper', 1, 2, 3, 4);
-  myBooks.forEach(title => console.log(title);
-}
-
-function overloadDemo() {
-  console.log(GetTitles(true));
-  console.log(GetTitles('Herman Melville'));
-  // No: there is no function signature matching the next call.
-  // GetTitles(42);
-}
+//******************************************************************************
 
-// defaultParamsDemo();
-// functionTypeDemos();
-// functionCallDefaultParamsDemo();
-// restParamsDemo();
-overloadDemo();
+let myBook: Book = {
+  author: 'Jane Austen',
+  available: true,
+  category: Category.Fiction,
+  // copies: 3,
+  id: 5,
+  pages: 250,
+  title: 'Pride and prejudice',
+  // year: '1813',
+  markDamaged: reason => console.log(`Damaged: ${reason}.`),
+};
+
+PrintBook(myBook);
+myBook.markDamaged('Missing back cover');

+ 11 - 0
app/enums.ts

@@ -0,0 +1,11 @@
+enum Category {
+  Biography,     // 0
+  Poetry,        // 1
+  Fiction,       // 2
+  History,       // 3
+  Children,      // 4
+}
+
+export {
+  Category,
+}

+ 16 - 0
app/interfaces.ts

@@ -0,0 +1,16 @@
+import { Category } from "./enums";
+
+interface Book {
+  author: string,
+  available: boolean,
+  category: Category;
+  id: number,
+  pages?: number,
+  title: string,
+
+  markDamaged: (reason: string) => void,
+}
+
+export {
+  Book,
+}