2

I would like to create a interface which receives a actual class not a instance of the class. something like this:

class checkIfGonextPage{
  localResult;
  next;
  constructor(localResult:string,next:string){
    this.localResult=localResult;
    this.next=next;
  }
}
 interface comp{
    CheckifCanGoNext: checkIfGonextPage;
}
let x:comp ={"CheckifCanGoNext":checkIfGonextPage}

let f = new x["CheckifCanGoNext"]("sss","")
console.log(f.localResult);

if I set CheckifCanGoNext to any it will work but I will loose the type.

1 Answers1

0

You can always use the typeof type query operator to get the type of a particular named value. If you want to put the actual value named CheckIfGonextPage, the class constructor, in the CheckifCanGoNext property of a Comp, then you can refer to that type as typeof CheckIfGonextPage:

interface Comp {
    CheckifCanGoNext: typeof CheckIfGonextPage;
}
let x: Comp = { "CheckifCanGoNext": CheckIfGonextPage }    
let f = new x["CheckifCanGoNext"]("sss", "")

You could also use a construct signature to describe something that can be called with the new operator. It looks like a function call signature but with the word new before it:

interface Comp {
    CheckifCanGoNext: new (localResult: string, next: string) => CheckIfGonextPage
}
let x: Comp = { "CheckifCanGoNext": CheckIfGonextPage }

let f = new x["CheckifCanGoNext"]("sss", "");

Either way should work.

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360