-
Notifications
You must be signed in to change notification settings - Fork 15
Дюкин Петр, ts-1 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,3 @@ | ||
| /** Задача 2 | ||
| * Требуется реализовать функцию filter, которая будет принимать | ||
| * массив с объектами 3х типов | ||
| * наименование типа | ||
| * возврщать массив с объектами, которые имеют тип, указанный во втором аргументе | ||
| */ | ||
|
|
||
| enum System { | ||
| Linux = 0, | ||
| Window = 1, | ||
|
|
@@ -71,6 +64,11 @@ const obj7: ThirdType = { | |
| const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7]; | ||
|
|
||
| function filter(array: Array<FirstType | SecondType | ThirdType>, type: string) { | ||
| let res: any[] = []; | ||
| array.forEach(item => { | ||
| if (typeof item === type) res.push(item) | ||
| }); | ||
| return res; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Работать не будет, т.к. в рантайме у x будет тип Object => ни один объект не попадет результирующий массив |
||
|
|
||
| filter(array, 'FirstType'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,6 @@ | ||
| /** Задача 3 | ||
| * Требуется реализовать функцию add, которая будет иметь 2 сигнатуры | ||
| * 1 - принимает 2 аргумента: x, y оба типа string и возвращает тип string | ||
| * 2 - принимает 2 аргумента: x, y оба типа number и возвращает тип number | ||
| * использовать тип any для типизации параметров запрещено | ||
| * функция должна возвращать сумму двух аргументов | ||
| */ | ||
| function add(x: string, y: string): string; | ||
| function add(x: number, y: number): number; | ||
| function add(x: string | number, y: string | number): string | number { | ||
| return typeof x === "string" ? <string>x + <string>y : <number>x + <number>y; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачтено на 3 балла |
||
| add('20', '21'); //2021 | ||
| add(20, 21); //41 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,3 @@ | ||
| /** Задача 4 | ||
| * Разобраться и описать в чём заключается разница между IFoo и FooType | ||
| * (фактически нужно описать в чём разница между type и interface) | ||
| * + к карме, если приведете примеры | ||
| */ | ||
| interface IFoo { | ||
| a: number | ||
| b: string | ||
|
|
@@ -12,3 +7,11 @@ type FooType = { | |
| a: number | ||
| b: string | ||
| }; | ||
|
|
||
| /** | ||
| * Возможно, это будет несовсем честно, но еще неск. месяцев назад рассматривал эту статью | ||
| * (https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types) из-за чего | ||
| * мое понимание в их различии стало именно таким. Думаю, что нет смысла копировать и переводить | ||
| * материал оттуда, да и врать, что взял все это из головы - тоже..) | ||
| * В любом случае извините...) | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачтено на 3 балла |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,17 @@ | ||
| /** Задача 4 | ||
| * Измените объявление функции filterUsers так, чтобы | ||
| * в аргумент criteria можно было передавать объект, | ||
| * содержащий любое поле или поля объекта User | ||
| */ | ||
|
|
||
| interface User { | ||
| type: string; | ||
| name: string; | ||
| age: number; | ||
| occupation: string; | ||
| } | ||
|
|
||
| interface HybridUser { | ||
| type?: string; | ||
| name?: string; | ||
| age?: number; | ||
| occupation?: string; | ||
| } | ||
|
|
||
| interface Admin { | ||
| type: string; | ||
| name: string; | ||
|
|
@@ -73,7 +74,7 @@ export function logPerson(person: Person) { | |
| console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`); | ||
| } | ||
|
|
||
| export function filterUsers(persons: Person[], criteria: User): User[] { | ||
| export function filterUsers(persons: Person[], criteria: HybridUser): User[] { | ||
| return persons.filter(isUser).filter((user) => { | ||
| const criteriaKeys = Object.keys(criteria); | ||
| return criteriaKeys.every((fieldName) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачтено на 3 балла |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачтено на 3 балла