Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/task_2/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
/** Задача 1
* Реализовать декоратор, используя встроенную поддержку декораторов в TypeScript,
* который будет реагировать на присвоение в поле email значения.
* Когда присваивается корректный e-mail в консоль выводится сообщение email valid.
* Когда присваивается некорректный e-mail возбуждается ошибка.
*/
function isValidEmail(target: Object, propertyKey: string): any {
let email: string;
let descriptor: PropertyDescriptor = {
set(emailToCheck: string) {
if (!/[a-zA-Z0-9]+@[a-zA-Z]+.[a-zA-Z]+/.test(emailToCheck))
throw 'email is invalid'
console.log('email is valid');
email = emailToCheck;
}
}
return descriptor;
}

class Example {
@isValidEmail
public email: string = "";
}

let exampleInstance = new Example();
exampleInstance.email = "fkkldfjg"; // генерирует эксепшен
exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid
exampleInstance.email = "fkkldfjg";
exampleInstance.email = "misha@mail.ru";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

засчитано 1 балл

11 changes: 5 additions & 6 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/** Задача 4
* Описать каким должен быть объект X, чтобы метод работал корректно
*/

function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}

const x = undefined;
//Здесь могло бы быть что угодно, но почему бы не оставить фамилию замечательного человека? :)
const x = {
m : "Обабков"
}

console.log(getProperty(x, "m"));
console.log(getProperty(x, "m"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

засчитано 1 балл