2

I've seen an API list methods with both of the following signatures:

methodA(...) :        ReturnType

methodB(...) : typeof ReturnType

I understand the first but not the second.

My question comes from reading through the API for Angular2 where I encounter both of the above. For example, class TestBed lists two methods, initTestEnvironment and configureTestingModule, as:

initTestEnvironment(ngModule: Type<any>, platform: PlatformRef) : TestBed

configureTestingModule(moduleDef: TestModuleMetadata) : typeof TestBed

My understanding is that the first line means that when you call this method the return value will be an object of type TestBed. That makes sense to me.

However, I don't understand the type of the return value in the second line. Is the return value of this method an object of type typeof TestBed? The JS operator typeof returns a string, so how can you have an object of type "Anything" instead of Anything, i.e. a string versus a class/interface/etc.?

I am primarily interested in an explanation of this method signature syntax. However I would also very much appreciate a link to an (official?) online source describing this shorthand/syntax.

By the way, I presume that the syntax of this method signature is specific to TypeScript, but please correct me if I'm wrong about that.

(I have attempted googling this question several ways, including keeping an eye out for StackExchange discussions, and have come up empty-handed. There is a question on StackOverflow that asks "What is the return type of typeof?" which is completely unrelated. Other searches quickly disintegrate into Google separating "typeof" into "type of" which is also completely unhelpful.)

8
  • Do you know what TestBed is? Is it a variable/object or interface/class/enum?
    – Erik Eidt
    Oct 4, 2016 at 14:58
  • 1
    Have a read of typescriptlang.org/docs/handbook/classes.html. Specifically the bit from "Next, we then use the class directly. Here we create a new variable called greeterMaker..." Hopefully it'll make more sense to you than it does to me! :)
    – David Arno
    Oct 4, 2016 at 15:23
  • 1
    is it not typescript rather than javascript? blog.mariusschulz.com/2016/05/31/…
    – Ewan
    Oct 4, 2016 at 15:33
  • 1
    fyi, the construct typeof variable is also supported in TypeScript, so you can match the type of an object declared by fields instead of by interface.
    – Erik Eidt
    Oct 4, 2016 at 16:01
  • 1
    @AndrewWillems: TypeScript is essentially C# in Javascript clothing (with some notable differences), and typeof is a keyword in C#. Oct 4, 2016 at 17:34

1 Answer 1

3

A method signature returning typeof returns the constructor of the specified type, so that you can new it.

4
  • I'm trying to understand what the 2 different types actually represent. Let's say that I have methodA(): MyClass and methodB(): typeof MyClass. Is the following correct?: (1) the type of methodA includes the instance properties and instance methods of MyClass while (2) the type of methodB includes the constructor function as well as the static properties and static methods of MyClass. Oct 4, 2016 at 18:03
  • That makes sense to me. I wish I could advise you further... my answer was simply my boss's response when I asked him your question. Oct 4, 2016 at 18:06
  • have you got a link to the documentation?
    – Ewan
    Oct 5, 2016 at 8:58
  • @Ewan, the best official documentation I've found so far is from @DavidArno's comment to my question, i.e. the TypeScript documentation for Classes. As David writes, the section on typeof is '...[s]pecifically the bit from "Next, we then use the class directly..."', near the bottom. I had read it before, but the typeof discussion had slipped past me. The section on typeof isn't long, so the comments/answers here definitely helped. (The link you provided in a comment to my question was also helpful...thx.) Oct 6, 2016 at 1:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.