Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I assign the typing of a super to the child constructor

I'm looking for a way to pass on the typings of a parent's constructor parameters into the child's constructor, e.g.

class B extends A {    
    constructor (input) {
        super(input);
    }
}

I tried

class B extends A {    
    constructor (input : ConstructorParameters<typeof super>) {
        super(input);
    }
}

But I get an error from eslint 'super' is not defined.

like image 863
A. L Avatar asked Nov 14 '25 18:11

A. L


1 Answers

I think you have to pass as ConstructorParameters<typeof A>[0] as a constructor param type.

class A {
  title: string;
  constructor(title: string) {
    this.title = title;
  }
}

class B extends A {
  constructor(title: ConstructorParameters<typeof A>[0]) {
    super(title);
  }
}

One more solution you can try suggested in comment:

  constructor(title: ConstructorParameters<typeof A>) {
    super(...title);
  }
like image 98
Rahul Sharma Avatar answered Nov 17 '25 09:11

Rahul Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!