From d136d6f667913e064c8d1b936d01aa3401b22dc6 Mon Sep 17 00:00:00 2001 From: Yaroslav Komarov Date: Thu, 1 Apr 2021 09:21:35 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B0=D1=80=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=AF=D1=80=D0=BE=D1=81=D0=BB=D0=B0=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 12 ++++++++++-- src/task_2/index.ts | 7 +++++++ src/task_3/index.ts | 7 +++++++ src/task_4/index.ts | 34 ++++++++++++++++++++++++++++++++++ src/task_5/index.ts | 2 +- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3edb7b9..e665e0d 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -3,8 +3,16 @@ * который написан в функции logObj компилировался и исполнялся корректно */ -type FooType = unknown; -type BarType = unknown; +type FooType = { + stringProp: string; + numberProp: number; + barObject: BarType; +}; +type BarType = { + stringsArrayProp: string[]; + numbersOrDatesArrayProp: Array; + functionProp: (flag: boolean) => void; +}; export const fooObjects: FooType[] = [ { diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 00b4939..ef5df11 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -71,6 +71,13 @@ const obj7: ThirdType = { const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7]; function filter(array: Array, type: string) { + let result: any[] = []; + array.forEach(obj =>{ + if (typeof obj === type){ + result.push(obj); + } + }); + return result; } filter(array, 'FirstType'); diff --git a/src/task_3/index.ts b/src/task_3/index.ts index b7c9cdc..452f213 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -7,6 +7,13 @@ */ function add(x: string, y: string): string; function add(x: number, y: number): number; +function add(x: number | string, y: number | string): number | string{ + if (typeof x === 'number' && typeof y === 'number'){ + return x + y; + } else { + return x + y; + } +} add('20', '21'); //2021 add(20, 21); //41 \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index e884df8..b9701a2 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -12,3 +12,37 @@ type FooType = { a: number b: string }; + +/*Следует понимать, что interface - инструмент создания сущностей в контексте ООП. + Интерфейс это определенный контракт, который обязуется испольнять объект/ класс, + реализующий этот интерфейс. +*/ + +class ClsFromInterface implements IFoo{ + a: 12; + b: '12'; +} + +let objFromInterface: IFoo = { + a: 12, + b: '12', +} + +//Класс может реализовывать интерфейс, но класс не может "реализовать type", +//type можно только объявлять. +//Не получится сделать что-то подобное: class Tmp: FooType; + +//Однако, тип подобно интерфейсу требует объявлять все поля, присущие данному типу. + +let objFromType: FooType = { + a: 12, + b: '12' +} + +//Пожалуй главное отличие - возможность наследовать интерфейсы. + +interface IFoo1 extends IFoo{ + //далее перечисляем собственные поля интерфейса IFoo1. + prop1: string; + prop2: number; +} \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index ef3e0c0..8564c9f 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -73,7 +73,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: {type?: string; name?: string; age?: number; occupation?: string;}): User[] { return persons.filter(isUser).filter((user) => { const criteriaKeys = Object.keys(criteria); return criteriaKeys.every((fieldName) => {