Home > other >  Accessing inline type in the constructor
Accessing inline type in the constructor

Time:11-09

I have this:

export class QueueEntity<T> implements HasInternalQueue<T> {
    
    
    opts: { // <--- inline type here
        foo: boolean
    }
    
    constructor(v: typeof this.opts) { // this doesn't quite work
        this.opts = v
    }

}

is there a way to reference the inline type or is this not possible?

CodePudding user response:

Since there is no way to define a type inside the class directly (An open issue in the TS repo). I think you can use the class name to reference this:

export class QueueEntity<T> {
   
    
    opts: { // <--- inline type here
        foo: boolean
    }
    
    constructor(v: QueueEntity<T>['opts']) { //  <-- should work now
        this.opts = v
    }

}

const obj = new QueueEntity({ foo : false });
const obj2 = new QueueEntity({ foo2 : false });
const obj3 = new QueueEntity();

Playground

  • Related