tannerntannern / ts-mixer

A small TypeScript library that provides tolerable Mixin functionality.
MIT License
379 stars 27 forks source link

Decorator deep inheritance not working #28

Closed MigasEu closed 3 years ago

MigasEu commented 4 years ago

I'm working with class validator and ts-mixer, but it seems to have a problem inheriting inherited decorators. It inherits the decorators from the parent, but not from grandparents.

Example:

class Disposable {
    @decorate(IsBoolean()) // instead of @IsBoolean()
    isDisposed: boolean = false;
}

class Statusable {
    @decorate(IsIn(['red', 'green'])) // instead of @IsIn(['red', 'green'])
    status: string = 'green';
}

class Statusable2 {
    @decorate(IsIn(['red', 'green'])) // instead of @IsIn(['red', 'green'])
    other: string = 'green';
}

class ExtendedObject extends Mixin(Disposable, Statusable) {}

class ExtendedObject2 extends Mixin(Statusable2, ExtendedObject) {}

const extendedObject = new ExtendedObject2();
extendedObject.status = 'blue';
extendedObject.other = 'blue';
extendedObject.isDisposed = undefined;

validate(extendedObject).then(errors => {
    console.log(errors);
});

This causes only validations from Statusable2 to be triggered, and the ones from ExtendedObject to be ignored.

A workaround is to do Mixin(Statusable2, Disposable, Statusable, ExtendedObject), but the best solution for maintainability.

tannerntannern commented 4 years ago

Thanks for reporting and for the code sample, that will make it quicker for me to test. I'll try to resolve this soon, but I do have a busy weekend so it may be a few days.

vanakema commented 3 years ago

Just bumping this, as we just ran into this issue too.

tannerntannern commented 3 years ago

Hello all, I'm sorry for taking so long to resolve this -- my life has been terribly busy lately and I haven't had time to really sit down and figure this one out until recently. This issue should be resolved in v5.4.0, and you can additionally customize how decorators are inherited. Closing this issue for now.

vanakema commented 3 years ago

Thanks @tannerntannern ! You're a god