Conversation
| @DecorateEn | ||
| public exec(): string { | ||
| return (this.a + this.b).toString(); | ||
| } |
There was a problem hiding this comment.
это не реализация патерна декоратор
https://refactoring.guru/ru/design-patterns/decorator/typescript/example
| class Example { | ||
| @decorEmail | ||
| public email: string = ""; | ||
| } |
| } | ||
|
|
||
| public register<?>(type: ?) { | ||
| public register(type: Function) { |
There was a problem hiding this comment.
почему убрал использование generic-типа?
теперь в этот метод можно передать любой класс, который будет не унаследован от Control
| const x = undefined; | ||
| const x = { | ||
| m: "", | ||
| }; |
|
|
||
| @validate(ValueExample2, "booleanProp") | ||
| public propValueExample2: any; | ||
| } |
There was a problem hiding this comment.
прикладываю правильное решение
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) { |
There was a problem hiding this comment.
Сюда приходит не тип а объект, для проверки типа недо было использовать generic тип устанавливаемый в момент инициализации объекта.
No description provided.