1

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.

3
  • 1
    Why not ` klass: { get: typeof Application["get"] }` get is of the same type (in this case same siganture) with typeof Application["get"]. Your mapped type solution would also work but would take all members from Application which I am not sure you want Commented May 10, 2019 at 10:46
  • Yeah, this looks good. My class if somewhat more complex though, so I had also to do klass: { get: ..., new (): Application } (otherwise I got some errors about this context being wrong. Anyway, I think I want all members of that class, since it is that class. Commented May 10, 2019 at 11:23
  • @TitianCernicova-Dragomir please post your comment as an answer, then I'll accept it because it technically answers my question! (picking up all properties was not asked in the first place) Commented May 10, 2019 at 12:00

2 Answers 2

5

You can reference the type of the static method using typeof Class['methodName']. You can use this type directly as the type for get:

class Caller {
  klass: {
    get: typeof Application["get"]
  }
}

This will mean get as the same type as the method get of Application. typeof Application["get"] is the whole function signature of the method, so any changes to parameters or return type will be reflected in the get on klass

Sign up to request clarification or add additional context in comments.

Comments

0

You could define get's type in the same file, like this:

type ApplicationGetType = (arg: string) => string;

Then use that in both function definitions, then any changes only have to be done once, to the type definitions.

You could also try Parameters<Application['get']> and ReturnType<Application['get']> to get the correct types when referencing that method.

1 Comment

Thanks for your answer. Though though I'd avoid to create extra types if possible. Because my class actually is my type definition. Good idea to use Parameters and ReturnType though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.