Skip to content

Commit 8af072c

Browse files
committed
fix: description
1 parent fb83d44 commit 8af072c

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"printWidth": 80,
23
"singleQuote": true
34
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { test, expect } from 'vitest';
2-
import { filterByProperty, filteredBooksByAuthor, filteredVideosByLength } from './type-operator';
2+
import {
3+
filterByProperty,
4+
filteredBooksByAuthor,
5+
filteredVideosByLength,
6+
} from './type-operator';
37

8+
test('should return 1 book authored by Philip K. Dick', () => {
9+
expect(filteredBooksByAuthor.length).toBe(1);
10+
});
411

5-
test('should return 1 book authored by Philip K. Dick', () => {
6-
expect(filteredBooksByAuthor.length).toBe(1);
7-
});
8-
9-
test('should return only some specific videos', () => {
10-
expect(filteredVideosByLength.length).toBe(2);
11-
});
12+
test('should return only some specific videos', () => {
13+
expect(filteredVideosByLength.length).toBe(2);
14+
});

src/type-operator/type-operator.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Utility Types
55
* ------------------
66
*
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.
88
*
99
* Hint: https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
1010
*/
@@ -27,32 +27,34 @@ const books: Book[] = [
2727
{
2828
id: 2,
2929
name: 'When: The Scientific Secrets of Perfect Timing',
30-
author: 'Daniel H. Pink'
30+
author: 'Daniel H. Pink',
3131
},
3232
{
3333
id: 3,
3434
name: 'Total Recall: My Unbelievably True Life Story',
35-
author: 'John Doe'
35+
author: 'John Doe',
3636
},
37-
{ id: 4, name: 'Wyloguj swój mózg', author: 'Anders Hansen' }
37+
{ id: 4, name: 'Wyloguj swój mózg', author: 'Anders Hansen' },
3838
];
3939

4040
const videos: Video[] = [
4141
{ id: 1, name: 'Ciekawostki o typach', length: 17 },
4242
{ id: 2, name: 'Refaktoryzacja JS do TS', length: 15 },
4343
{ id: 3, name: 'TypeScript na Backendzie', length: 13 },
4444
{ 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 },
4646
];
4747

4848
function filterByProperty<T extends IdentifiableItem, K extends keyof T>(
4949
items: T[],
5050
key: K,
51-
value: T[any]
51+
value: unknown
5252
): T[] {
53-
return items.filter(item => item[key] === value);
53+
return items.filter((item) => item[key] === value);
5454
}
5555

56-
export const filteredBooksByAuthor = filterByProperty(books, 'author', {author:'Philip K. Dick'});
56+
export const filteredBooksByAuthor = filterByProperty(books, 'author', {
57+
author: 'Philip K. Dick',
58+
});
5759

5860
export const filteredVideosByLength = filterByProperty(videos, 'length', '15');

0 commit comments

Comments
 (0)