Angularjs 也充当函数类型的TypeScript类

Angularjs 也充当函数类型的TypeScript类,angularjs,function,class,typescript,Angularjs,Function,Class,Typescript,在尝试实现angular的IHttpService时,我不确定如何处理以下功能 interface IHttpService { <T>(config: IRequestConfig): IHttpPromise<T>; } class MyHttpService implements IHttpService { // The following does not work <T>(config: IRequestConfig):

在尝试实现angular的IHttpService时,我不确定如何处理以下功能

interface IHttpService {
    <T>(config: IRequestConfig): IHttpPromise<T>;
}

class MyHttpService implements IHttpService
{
    // The following does not work
    <T>(config: IRequestConfig): IHttpPromise<T> 
    {
    }
}
接口IHttpService{
(config:IRequestConfig):ihttpromise;
}
类MyHttpService实现IHttpService
{
//以下方法不起作用
(配置:IRequestConfig):IHttpPromise
{
}
}
这可能吗?

使用TypeScript类无法做到这一点。您需要退回到使用简单函数

interface IHttpService {
    <T>(config: IRequestConfig): IHttpPromise<T>;
}

class MyHttpService implements IHttpService
{
    // The following does not work
    <T>(config: IRequestConfig): IHttpPromise<T> 
    {
    }
}

并不是所有可以在TypeScript中定义的接口都可以使用TypeScript类实现。这就是其中之一

basarat认为应该使用常规函数来实现
IHttpService
接口是正确的

以下是实现该接口并在Angular中使用它的一个示例,以供将来参考:

interface IRequestConfig {}
interface IHttpPromise<T> {
    then: (resolve?: (value: T) => any, reject?) => IHttpPromise<T>;
}

interface IHttpService {
    <T>(config: IRequestConfig): IHttpPromise<T>;
}


function MyHttpService<T>(config: IRequestConfig): IHttpPromise<T>{
    // Define service behaviour here.
}


angular.module('MyModule')
    .service('MyHttpService', MyHttpService)
    .controller('MyController', function(MyHttpService: IHttpService){

        MyHttpService<MyType>({
            // Implement `IRequestConfig` here. 
        }).then(function(value){
            // Accces properties on `value`.
        });

    });
接口IRequestConfig{}
接口IHttpPromise{
然后:(解析?:(值:T)=>any,reject?=>IHttpPromise;
}
接口IHTTP服务{
(config:IRequestConfig):ihttpromise;
}
函数MyHttpService(配置:IRequestConfig):IHttpPromise{
//在这里定义服务行为。
}
angular.module('MyModule')
.service('MyHttpService',MyHttpService)
.controller('MyController',函数(MyHttpService:IHttpService){
MyHttpService({
//在此处实现'IRequestConfig'。
}).然后(函数(值){
//访问“值”上的属性。
});
});