diff --git a/package-lock.json b/package-lock.json index c6a18e7..1820d32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,40 @@ { "name": "ts-task-1", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 3, "requires": true, - "dependencies": { - "tsc": { + "packages": { + "": { + "name": "ts-task-1", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "tsc": "^1.20150623.0", + "typescript": "^5.4.5" + }, + "devDependencies": {} + }, + "node_modules/tsc": { "version": "1.20150623.0", "resolved": "https://registry.npmjs.org/tsc/-/tsc-1.20150623.0.tgz", - "integrity": "sha1-Trw8d04WkUjLx2inNCUz8ILHpuU=" + "integrity": "sha1-Trw8d04WkUjLx2inNCUz8ILHpuU=", + "deprecated": "you probably meant to install typescript", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + } + }, + "node_modules/typescript": { + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", + "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } } } } diff --git a/package.json b/package.json index 0d703f8..f296662 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ }, "homepage": "https://github.com/RTF-Angular-2021/ts-task-1#readme", "dependencies": { - "tsc": "^1.20150623.0" - }, - "devDependencies": {} + "tsc": "^1.20150623.0", + "typescript": "^5.4.5" + } } diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3edb7b9..988edea 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: Array, + 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 fc437d8..41c51b0 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -1,30 +1,30 @@ /** Задача 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 +70,34 @@ const obj7: ThirdType = { const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7]; -function filter(anyObjectArray: Array) { - +function filter(array: Array, type: string) { + let result: any[] = []; + switch(type){ + case "FirstType": + array.forEach(item =>{ + if(typeof item.prop1 == 'string' && typeof item.prop2 == 'boolean' && !("prop3" in item)){ + result.push(item); + } + }); + break; + case 'SecondType': + array.forEach(item =>{ + if(typeof item.prop1 == 'undefined' && typeof item.prop2 == 'function'){ + result.push(item); + } + }); + break; + case 'ThirdType': + array.forEach(item =>{ + if(typeof item.prop1 == 'string' && typeof item.prop2 == 'boolean' && "prop3" in item){ + result.push(item); + } + }); + break; + } + return result; } -filter(array); -filter(array); -filter(array); +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..3eccbd1 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -5,8 +5,18 @@ * использовать тип any для типизации параметров запрещено * функция должна возвращать сумму двух аргументов */ -function add(x: string, y: string): string; -function add(x: number, y: number): number; +type srtOrNum = string|number; +function add(x: srtOrNum , y: srtOrNum): srtOrNum{ + if(typeof x == "string" && typeof y == "string"){ + return x + y; + } + else if(typeof x == "number" && typeof y == "number"){ + return x + y; + } + else{ + throw new Error('err'); + } +}; 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..2f6f609 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -3,12 +3,28 @@ * (фактически нужно описать в чём разница между type и interface) * + к карме, если приведете примеры */ -interface IFoo { - a: number - b: string + +//Interface это всегда объект, а Type может быть другими сущностями. +//interface мы можем расширять(наследовать) например: +interface Animal { + name: string } + +interface Dog extends Animal { + bark: () => void; +} + +const dog: Dog = { + name: 'Бобик', + bark: () => console.log('Гав') +} + +// interface IFoo { +// a: number +// b: string +// } -type FooType = { - a: number - b: string -}; +// type FooType = { +// a: number +// b: string +// }; 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) => {