diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3edb7b9..cc7d3ef 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: (a: boolean) => void +}; export const fooObjects: FooType[] = [ { diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 00b4939..70eb739 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -11,6 +11,8 @@ enum System { MacOS = 2, } +type typeSystem = keyof typeof System; + type FirstType = { prop1: string, prop2: boolean, @@ -70,9 +72,31 @@ const obj7: ThirdType = { const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7]; -function filter(array: Array, type: string) { +type ArrayItem = FirstType | SecondType | ThirdType; + +function filter(array: ArrayItem[], type: 'FirstType' | 'SecondType' | 'ThirdType'): ArrayItem[] { + return array.filter(item => { + if (type === "ThirdType" && (typeof (item as ThirdType).prop3 === "number")) { + return item; + } else if (type === "SecondType" && (typeof (item as SecondType).prop2 !== "boolean")) { + return item; + } else if (type === "FirstType" && (typeof (item as FirstType).prop2 === "boolean" && lengthObject(item) === 2)) { + return item; + } else { + return false; + } + }); +} + +function lengthObject(item: object): number { + let length = 0; + for (let i in item) { + length++ + } + return length; } filter(array, 'FirstType'); filter(array, 'SecondType'); filter(array, 'ThirdType'); + diff --git a/src/task_3/index.ts b/src/task_3/index.ts index b7c9cdc..cecbe6a 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -7,6 +7,12 @@ */ function add(x: string, y: string): string; function add(x: number, y: number): number; - +function add(x: string | number, y: string | number): string | number { + if ( typeof x === "string" && typeof y === "string") { + return x + y + } else if (typeof x === "number" && typeof y === "number") { + 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..b63baeb 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -7,8 +7,61 @@ interface IFoo { a: number b: string } - + type FooType = { a: number b: string }; + +// 1. Интерфейс может быть использован в выражении extends +interface IFoo { + x: number + b: string +} +interface B extends IFoo { + c: boolean +} + +// 2. Интерфейсы могут быть дополнены +interface IFoo { + m: boolean; +} +interface IFoo { + n: symbol; +} + +//3. Интерфейс может наследовать тип +interface IFoo extends FooType { a: number; b: string } + +// 3. Тип может использоваться в другом типе +type newType = FooType + +//4. Тип тоже может наследоваться, запись следующая: +type Point = FooType & { a: number; b: string } + +//5. Тип может наследовать интерфейс +type Foo = IFoo & { y: number; b: string }; + +//6. После того как тип объявили, его уже нельзя изменить, интерфейс можно + +//6. Implements работает для type и для interface +interface Point1 { + x: number; + y: number; +} + +class SomePoint implements Point1 { + x = 1; + y = 2; +} + +type Point2 = { + x: number; + y: number; +}; + +class SomePoint2 implements Point2 { + x = 1; + y = 2; +} + diff --git a/src/task_5/index.ts b/src/task_5/index.ts index ef3e0c0..dbadc97 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: Partial): User[] { return persons.filter(isUser).filter((user) => { const criteriaKeys = Object.keys(criteria); return criteriaKeys.every((fieldName) => {