From af068532bee6e351213e5a0b6d62150086c466b7 Mon Sep 17 00:00:00 2001 From: evgeniy Date: Thu, 1 Apr 2021 14:30:06 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=D1=81=D0=BA=D0=B8=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.ts | 13 +++++++++++-- src/task_3/index.ts | 9 +++++++++ src/task_4/index.ts | 24 ++++++++++++++++++++++++ src/task_5/index.ts | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3edb7b9..5e58b6d 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -3,8 +3,17 @@ * который написан в функции logObj компилировался и исполнялся корректно */ -type FooType = unknown; -type BarType = unknown; +type FooType = { + stringProp: string, + numberProp: number, + barObject: BarType, +}; + +type BarType = { + stringsArrayProp: Array, + numbersOrDatesArrayProp: Array, + functionProp: Function, +}; export const fooObjects: FooType[] = [ { diff --git a/src/task_3/index.ts b/src/task_3/index.ts index b7c9cdc..18e2ec0 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -8,5 +8,14 @@ function add(x: string, y: string): string; function add(x: number, y: number): number; +function add(x, y) { + if (typeof x === "number" && typeof y === "number") { + return Number(x + y); + } + if (typeof x === "string" && typeof y === "string") { + return (x + y).toString(); + } +} + 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..507868a 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -7,8 +7,32 @@ interface IFoo { a: number b: string } + +/* Пример interface: +Только описывает +interface IVegetable { + name: string; + getCalories(): number; +} +Это конкретный класс +class Tomato implements IVegetable { + name: string = 'Tomato'; + getCalories() { + return 18; + } +} +*/ +//Интерфейс может быть назван в выражении extends или implement, но псевдоним типа для литерала типа объекта не может. +//Интерфейс может иметь несколько объединенных объявлений, но псевдоним типа для литерала типа объекта не может. type FooType = { a: number b: string }; + +/* +Type - это лишь типизация какой-то переменной, как правило, нестандартной, Например, мы хотим определить тип, который будет описывать степень готовности стейков: +type SteakRoast = 'medium' | 'rare' | 'welldone'; +Таким образом, мы можем присвоить переменной типа SteakRoast только эти предопределённые значения. +*/ + diff --git a/src/task_5/index.ts b/src/task_5/index.ts index ef3e0c0..ca50756 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: User | Object): User[] { return persons.filter(isUser).filter((user) => { const criteriaKeys = Object.keys(criteria); return criteriaKeys.every((fieldName) => {