-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericFunctions.ts
More file actions
49 lines (35 loc) · 1.16 KB
/
GenericFunctions.ts
File metadata and controls
49 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Book } from "./models/Book";
import { Customer } from "./models/Customer";
const someBook = new Book("Old Lady", "$150", 2019);
const customer = new Customer("Shubham", "Dhage");
function getItemFromDB<T>(item: T): T {
return item;
}
function getCustomerFromDB<T>(customer: T): T {
return customer;
}
// single type
function checkOut<T>(item: T): T {
const availableItem: T = getItemFromDB(item);
if (availableItem) {
// check out item for cutsomer
}
return item;
}
checkOut<Book>(someBook);
// multiple types
function checkOutMultiple<T, V>(item: T, customer: V): T {
const availableItem: T = getItemFromDB<T>(item);
const activeCustomer: V = getCustomerFromDB<V>(customer);
if (availableItem && activeCustomer) {
// check out item for cutsomer
}
return item;
}
checkOutMultiple<Book, Customer>(someBook, customer);
// SHorten Array
function shortenArray<T>(data: Array<T>, amountToShorten: number): Array<T> {
return data.splice(amountToShorten, data.length);
}
let stringArray: string[] = ["Nayan", "Shubham", "Priyanka", "Sharath", "Rubini"];
shortenArray<string>(stringArray, 2); // T --> string