Ver código fonte

6.8: interface for function types.

Frederic G. MARAND 5 anos atrás
pai
commit
5d338f6217
2 arquivos alterados com 14 adições e 6 exclusões
  1. 8 5
      app/app.ts
  2. 6 1
      app/interfaces.ts

+ 8 - 5
app/app.ts

@@ -1,4 +1,4 @@
-import {Book} from './interfaces';
+import {Book, DamageLogger} from './interfaces';
 import {Category} from "./enums";
 
 function GetAllBooks(): Book[] {
@@ -75,8 +75,8 @@ function CreateCustomer(name: string, age?: number, city?: string) {
   }
 }
 
-function CreateCustomerId(name: string, id: number): string {
-  return name + id;
+function CreateCustomerId(chars: string, nums: number): string {
+  return chars + nums;
 }
 
 function GetBookTitlesByCategory(categoryFilter: Category = Category.Fiction): Array<string> {
@@ -153,5 +153,8 @@ let myBook: Book = {
   markDamaged: reason => console.log(`Damaged: ${reason}.`),
 };
 
-PrintBook(myBook);
-myBook.markDamaged('Missing back cover');
+// PrintBook(myBook);
+// myBook.markDamaged('Torn back cover');
+
+let logDamage: DamageLogger = reason => console.log(`Damage reported: ${reason}.`);
+logDamage('Coffee stains');

+ 6 - 1
app/interfaces.ts

@@ -1,5 +1,9 @@
 import { Category } from "./enums";
 
+interface DamageLogger {
+  (reason: string): void;
+}
+
 interface Book {
   author: string,
   available: boolean,
@@ -8,9 +12,10 @@ interface Book {
   pages?: number,
   title: string,
 
-  markDamaged: (reason: string) => void,
+  markDamaged?: DamageLogger,
 }
 
 export {
   Book,
+  DamageLogger,
 }