Skip to content

Моисеев Михаил#11

Open
JIiCeHoK wants to merge 1 commit intoRTF-Angular-2021:masterfrom
JIiCeHoK:Moiseev-Mikhail
Open

Моисеев Михаил#11
JIiCeHoK wants to merge 1 commit intoRTF-Angular-2021:masterfrom
JIiCeHoK:Moiseev-Mikhail

Conversation

@JIiCeHoK
Copy link

No description provided.

@DecorateEn
public exec(): string {
return (this.a + this.b).toString();
}

Choose a reason for hiding this comment

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

это не реализация патерна декоратор
https://refactoring.guru/ru/design-patterns/decorator/typescript/example

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

Choose a reason for hiding this comment

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

3 бала

}

public register<?>(type: ?) {
public register(type: Function) {

Choose a reason for hiding this comment

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

почему убрал использование generic-типа?
теперь в этот метод можно передать любой класс, который будет не унаследован от Control

const x = undefined;
const x = {
m: "",
};

Choose a reason for hiding this comment

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

3 бала


@validate(ValueExample2, "booleanProp")
public propValueExample2: any;
}

Choose a reason for hiding this comment

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

прикладываю правильное решение
function validate<T, P extends keyof T>(type: new () => T, prop: P): (target: Object, propertyKey: string | symbol) => any {
let temp: T;
return (target: Object, propertyKey: string | symbol): PropertyDescriptor => {
let descriptor: PropertyDescriptor = {
get: function () {
return temp;
},
set: function (val: T) {
if (!(val instanceof type)) {
throw new Error("Устанавливается значение которое не соотвтетсвует типу");
}
if (temp[prop]) {
throw new Error("У требуемого поля не верный примитивный тип");
}
console.log("value valid");
temp = val;
}
};

    return descriptor;
};

}

class ValueExample1 {
public value: string;
public id: number;
}

class ValueExample2 {
public prop3: undefined;
public prop2: boolean;
}

class Example {
@Validate(ValueExample1, "id1")
public propValueExample1: any;

@validate(ValueExample2, "prop3")
public propValueExample2: any;

}

let ex1 = new Example();

let objValExample = new ValueExample1();
objValExample.id = 1;
objValExample.value = "qwe";
ex1.propValueExample1 = objValExample;

ex1.propValueExample2 = false;

return type;
},
set(newType) {
if (newType === type) {

Choose a reason for hiding this comment

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

Сюда приходит не тип а объект, для проверки типа недо было использовать generic тип устанавливаемый в момент инициализации объекта.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants