-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9-generics.ts
More file actions
53 lines (43 loc) · 1.42 KB
/
9-generics.ts
File metadata and controls
53 lines (43 loc) · 1.42 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
50
51
52
53
//using generics you can use the same function for 2 or more types
function WhatIsThisType<T>(value: T): string {
return typeof(value);
}
//when you call the fcuntion you can specify the type that you want to use
console.log(WhatIsThisType<number>(100));
console.log(WhatIsThisType<boolean>(true));
console.log(WhatIsThisType<string>("string value"));
//you can use interfaces in conjuntion with generics
interface collectionList<T> {
id: number,
name: string,
description: string,
items: T[] | undefined | null
}
//When you use the previous interface you can specify the type for the collections and items property
var newCollection : collectionList<number> = {
id: 1,
name: "New collection",
description: "This is a new collection",
items: [10, 20, 30]
}
//You can also extends using generics to include a spefic property by default in the type
interface HasId
{
id:number;
}
class Item implements HasId {
constructor(public id:number) {}
}
class GenericModel<T extends HasId> {
items: T[] | undefined
constructor(public name:string) {}
getItembyId(id:number) {
return this.items?.find(p=> p.id === id);
}
}
//when you want to set only some vlaues of the generic class you can use Partial
const simpleCollection = {
name: 'This is a partial collection',
items: ["Item1", "Item2"]
}
const partialCollection: Partial<collectionList<string>> = simpleCollection;