Description
It would be helpful if the typeof
keyword were changed to support more complex expressions, such as function calls.
Example
Given the following AngularJS service definition:
module myApp {
function myApiFactory($http: HttpSvc) {
return {
getSetting: (key: string) => $http.get(etc),
};
}
angular.service("myApi", myApiFactory);
}
It would be good if the following statement were possible:
export type MyApi = typeof myApiFactory();
Explanation
Since the function is a factory rather than the actual service, this type alias would other components to have a strongly-typed reference to the service. Though this example relates to AngularJS' service factory and dependency injection, this particular change would not be solely for AngularJS support.
Existing solutions
One option to resolve this issue is to explicitly define an interface for the service. However, this solution would require repeating the signature for all of the service's methods.
A workaround exists that relies on JavaScript's scoping rules to satisfy TypeScript's typeof
keyword. However, it significantly reduces readability and clarity. An example:
if (false)
var obj = myApiFactory(null);
export type MyApi = typeof obj;