From 9c99bc29c7dd158b7d36ab7e398cff56ea8b51d8 Mon Sep 17 00:00:00 2001 From: theSweater23 Date: Thu, 1 Apr 2021 14:02:51 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=83=D0=BB=D0=B5=D0=B9=D0=BC=D0=B0?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=20=D0=AD=D0=BC=D0=B8=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 6 +++--- src/task_2/index.ts | 27 ++++++++++++++------------- src/task_3/index.ts | 12 ++++++++++-- src/task_4/index.ts | 14 ++++++++++++++ src/task_5/index.ts | 2 +- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3edb7b9..34dee4b 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -2,9 +2,9 @@ * Требуется описать типы FooType и BarType так, чтобы код, * который написан в функции logObj компилировался и исполнялся корректно */ - -type FooType = unknown; -type BarType = unknown; +type numOrDate = Date | number; +type BarType = {stringsArrayProp: Array, numbersOrDatesArrayProp: Array, functionProp: Function}; +type FooType = {stringProp: string, numberProp: number, barObject: BarType}; export const fooObjects: FooType[] = [ { diff --git a/src/task_2/index.ts b/src/task_2/index.ts index fc437d8..680c65b 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -1,30 +1,31 @@ + /** Задача 2 - * Требуется реализовать функцию filter, которая будет - * принимать generic параметр - тип данных - * аргумент - массив с объектами каких-то типов - * возврщать массив с объектами, которые имеют тип, указанный в generic параметре + * Требуется реализовать функцию filter, которая будет принимать + * массив с объектами 3х типов + * наименование типа + * возврщать массив с объектами, которые имеют тип, указанный во втором аргументе */ enum System { Linux = 0, Window = 1, - MacOS = 2 + MacOS = 2, } type FirstType = { prop1: string, - prop2: boolean + prop2: boolean, } type SecondType = { prop1: typeof undefined, - prop2: () => Date + prop2: () => Date, } type ThirdType = { prop1: string, - prop2: boolean - prop3: System + prop2: boolean, + prop3: System, } const obj1: FirstType = { @@ -70,10 +71,10 @@ const obj7: ThirdType = { const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7]; -function filter(anyObjectArray: Array) { +function filter(array: Array, type: string) { } -filter(array); -filter(array); -filter(array); +filter(array, 'FirstType'); +filter(array, 'SecondType'); +filter(array, 'ThirdType'); \ No newline at end of file diff --git a/src/task_3/index.ts b/src/task_3/index.ts index b7c9cdc..79c93ed 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -8,5 +8,13 @@ function add(x: string, y: string): string; function add(x: number, y: number): number; -add('20', '21'); //2021 -add(20, 21); //41 \ No newline at end of file +function add(x: T, y: T): any { + let type: T; + if (typeof type === "number") + return (x as number) + (y as number); + else + return (x as string) + (y as string); +} + +console.log(add('20', '21')); //2021 +console.log(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..c1171b4 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -12,3 +12,17 @@ type FooType = { a: number b: string }; + + + // 1) Классы не могут наследоваться от type + + // 2) С помощью type можно создавать объединение типов + type example = number; + + type UnionWithType = FooType | example; + type UnionWithInterface = FooType | IFoo; + + // 3) В интерфейсы можно добавить поля после создания, а в type нет + // https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces + + // 4) Интерфейсы не могут расширять объединение type diff --git a/src/task_5/index.ts b/src/task_5/index.ts index ef3e0c0..3464ae0 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) => {