Skip to content

Commit 50b2b82

Browse files
committed
lesson-18
1 parent 05cb586 commit 50b2b82

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

public/app.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@ 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 = {
42+
uid: 1,
43+
resourceName: 'person',
44+
data: { name: 'shaun' }
45+
};
46+
const docFour = {
47+
uid: 1,
48+
resourceName: 'shoppingList',
49+
data: ['bread', 'milk']
50+
};
51+
console.log(docThree, docFour);

src/app.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,47 @@ form.addEventListener('submit', (e: Event) => {
2727
}
2828

2929
list.render(doc, type.value, 'end');
30-
});
30+
});
31+
32+
// GENERICS
33+
34+
// const addUID = (obj: object) => {
35+
// let uid = Math.floor(Math.random() * 100);
36+
// return {...obj, uid};
37+
// }
38+
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
55+
interface Resource<T> {
56+
uid: number;
57+
resourceName: string;
58+
data: T;
59+
}
60+
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+
};
72+
73+
console.log(docThree, docFour);

0 commit comments

Comments
 (0)