4
4
* Utility Types
5
5
* ------------------
6
6
*
7
- * Goal: Now we can add wrong value types as parameter to the function. Let's fix it by using one of the type operators .
7
+ * Goal: The third parameter of "filterByProperty" func has unknown type. Replace it with strict check of the type, based on prop name .
8
8
*
9
9
* Hint: https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
10
10
*/
@@ -27,32 +27,34 @@ const books: Book[] = [
27
27
{
28
28
id : 2 ,
29
29
name : 'When: The Scientific Secrets of Perfect Timing' ,
30
- author : 'Daniel H. Pink'
30
+ author : 'Daniel H. Pink' ,
31
31
} ,
32
32
{
33
33
id : 3 ,
34
34
name : 'Total Recall: My Unbelievably True Life Story' ,
35
- author : 'John Doe'
35
+ author : 'John Doe' ,
36
36
} ,
37
- { id : 4 , name : 'Wyloguj swój mózg' , author : 'Anders Hansen' }
37
+ { id : 4 , name : 'Wyloguj swój mózg' , author : 'Anders Hansen' } ,
38
38
] ;
39
39
40
40
const videos : Video [ ] = [
41
41
{ id : 1 , name : 'Ciekawostki o typach' , length : 17 } ,
42
42
{ id : 2 , name : 'Refaktoryzacja JS do TS' , length : 15 } ,
43
43
{ id : 3 , name : 'TypeScript na Backendzie' , length : 13 } ,
44
44
{ id : 4 , name : 'TypeScript i Frameworki Front-Endowe' , length : 19 } ,
45
- { id : 5 , name : 'Poznaj TypeScript' , length : 15 }
45
+ { id : 5 , name : 'Poznaj TypeScript' , length : 15 } ,
46
46
] ;
47
47
48
48
function filterByProperty < T extends IdentifiableItem , K extends keyof T > (
49
49
items : T [ ] ,
50
50
key : K ,
51
- value : T [ any ]
51
+ value : unknown
52
52
) : T [ ] {
53
- return items . filter ( item => item [ key ] === value ) ;
53
+ return items . filter ( ( item ) => item [ key ] === value ) ;
54
54
}
55
55
56
- export const filteredBooksByAuthor = filterByProperty ( books , 'author' , { author :'Philip K. Dick' } ) ;
56
+ export const filteredBooksByAuthor = filterByProperty ( books , 'author' , {
57
+ author : 'Philip K. Dick' ,
58
+ } ) ;
57
59
58
60
export const filteredVideosByLength = filterByProperty ( videos , 'length' , '15' ) ;
0 commit comments