Skip to content

Commit 6baa766

Browse files
committed
lesson-19
1 parent 50b2b82 commit 6baa766

File tree

2 files changed

+33
-58
lines changed

2 files changed

+33
-58
lines changed

public/app.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,24 @@ form.addEventListener('submit', (e) => {
2222
}
2323
list.render(doc, type.value, 'end');
2424
});
25-
// GENERICS
26-
// const addUID = (obj: object) => {
27-
// let uid = Math.floor(Math.random() * 100);
28-
// return {...obj, uid};
29-
// }
30-
// const addUID = <T extends object>(obj: T) => {
31-
// let uid = Math.floor(Math.random() * 100);
32-
// return {...obj, uid};
33-
// }
34-
const addUID = (obj) => {
35-
let uid = Math.floor(Math.random() * 100);
36-
return Object.assign(Object.assign({}, obj), { uid });
37-
};
38-
let docOne = addUID({ name: 'yoshi', age: 40 });
39-
//let docTwo = addUID('shaun');
40-
console.log(docOne.name);
41-
const docThree = {
25+
// ENUMS
26+
var ResourceType;
27+
(function (ResourceType) {
28+
ResourceType[ResourceType["BOOK"] = 0] = "BOOK";
29+
ResourceType[ResourceType["AUTHOR"] = 1] = "AUTHOR";
30+
ResourceType[ResourceType["FILM"] = 2] = "FILM";
31+
ResourceType[ResourceType["DIRECTOR"] = 3] = "DIRECTOR";
32+
})(ResourceType || (ResourceType = {}));
33+
;
34+
const docOne = {
4235
uid: 1,
43-
resourceName: 'person',
44-
data: { name: 'shaun' }
36+
resourceType: ResourceType.BOOK,
37+
data: { title: 'name of the wind' }
4538
};
46-
const docFour = {
47-
uid: 1,
48-
resourceName: 'shoppingList',
49-
data: ['bread', 'milk']
39+
const docTwo = {
40+
uid: 10,
41+
resourceType: ResourceType.DIRECTOR,
42+
data: { title: 'name of the wind' }
5043
};
51-
console.log(docThree, docFour);
44+
console.log(docOne);
45+
console.log(docTwo);

src/app.ts

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,26 @@ form.addEventListener('submit', (e: Event) => {
2929
list.render(doc, type.value, 'end');
3030
});
3131

32-
// GENERICS
32+
// ENUMS
3333

34-
// const addUID = (obj: object) => {
35-
// let uid = Math.floor(Math.random() * 100);
36-
// return {...obj, uid};
37-
// }
34+
enum ResourceType { BOOK, AUTHOR, FILM, DIRECTOR };
3835

39-
// const addUID = <T extends object>(obj: T) => {
40-
// let uid = Math.floor(Math.random() * 100);
41-
// return {...obj, uid};
42-
// }
43-
44-
const addUID = <T extends {name: string}>(obj: T) => {
45-
let uid = Math.floor(Math.random() * 100);
46-
return {...obj, uid};
47-
}
48-
49-
let docOne = addUID({name: 'yoshi', age: 40});
50-
//let docTwo = addUID('shaun');
51-
52-
console.log(docOne.name);
53-
54-
// with interfaces
5536
interface Resource<T> {
5637
uid: number;
57-
resourceName: string;
38+
resourceType: ResourceType;
5839
data: T;
5940
}
6041

61-
const docThree: Resource<object> = {
62-
uid: 1,
63-
resourceName: 'person',
64-
data: { name: 'shaun' }
65-
};
66-
67-
const docFour: Resource<string[]> = {
68-
uid: 1,
69-
resourceName: 'shoppingList',
70-
data: ['bread', 'milk']
71-
};
42+
const docOne: Resource<object> = {
43+
uid: 1,
44+
resourceType: ResourceType.BOOK,
45+
data: { title: 'name of the wind' }
46+
}
47+
const docTwo: Resource<object> = {
48+
uid: 10,
49+
resourceType: ResourceType.DIRECTOR,
50+
data: { title: 'name of the wind' }
51+
}
7252

73-
console.log(docThree, docFour);
53+
console.log(docOne);
54+
console.log(docTwo);

0 commit comments

Comments
 (0)