I have a class with a static method:
class Application {
static get(): string {
...
}
}
Now I want to reference this static get method in another class. I know I can do this:
class Caller {
klass: { get (): typeof Application["get"] }
}
This works easily if the method take no arguments. Edit: Please see below why this is wrong
Now if I add an argument:
class Application {
static get(argument: string): string {
...
}
}
... I also have to change Caller (and every other class having this signature):
class Caller {
klass: { get (argument: string): typeof Application["get"] }
}
Is there a way to avoid this? Because it is obvious that klass.get always follows the function signature of Application.get. Is there a way to tell typescript something like this:
class Caller {
klass: { get (signatureof typeof Application["get"]): typeof Application["get"] }
}
Edit: Actually I just realized the above is wrong: I actually defined get() to return something that behaves like typeof Application["get"].
I gave it a new shot with this:
class Caller {
klass: {
[Key in keyof typeof Application]: typeof Application[Key]
}
}
... though I have yet to see if this solves it, brb.
Edit 2: Both ways seem to be possible:
// reference all properties
class Caller {
klass: {
[Key in keyof typeof Application]: typeof Application[Key]
}
}
// or if only one specific thing is needed
// reference one property
class Caller {
klass: {
get: typeof Application["get"]
}
}
Unfortunately if the references method is more complex, e.g. get() accesses static properties defined on Application, this get more complicated though, since typescript will then complain about those not being found (if only the method is referenced, not every property).
So I think the way to go is to reference all properties to be on the safe side.
getis of the same type (in this case same siganture) withtypeof Application["get"]. Your mapped type solution would also work but would take all members fromApplicationwhich I am not sure you wantklass: { get: ..., new (): Application }(otherwise I got some errors aboutthis context being wrong. Anyway, I think I want all members of that class, since it is that class.