In a JavaScript method signature what is meant by a return type of `typeof blahBlahBlah`?

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.)


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


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *