Quantcast
Channel: How to extend class type with decorator - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to extend class type with decorator

$
0
0

I'd like to use decorators in my project. Here's what I have written:

export const DetachedWindow = (options: TDetachedWindowOptions = defaultOptions) => <R extends Constructable>(Clazz: R): R => {    const tmp = class extends Clazz {        value = 'value';        value2 = 'value2';        constructor(...args: any[]) {            super(...args);            // <some logick>        }    };    Object.defineProperty(tmp, 'name', { value: Clazz.name });    return tmp;};

As you see this decorators creates a few fields. But typescript can't recognize them

@DetachedWindow({ optA: 'optA' })export class SomeClass {    constructor() {        setTimeout(() => {            console.log(this.value); // TS2339: Property 'value' does not exist on type 'SomeClass'.        });    }}

It does exsist though. If I add @ts-ignore before using these parameters, the code works okay.

I wounder, how can I create a class decorator, that would extend parent's type. I tried to do something like this:

export const DetachedWindow = (options: TDetachedWindowOptions = defaultOptions) => <R extends Constructable>(Clazz: R): R & { value: string } => {

But it didn't help.

Any ideas?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images