Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Typescript中创建扩展的泛型返回类型_Typescript_Typescript2.0 - Fatal编程技术网

如何在Typescript中创建扩展的泛型返回类型

如何在Typescript中创建扩展的泛型返回类型,typescript,typescript2.0,Typescript,Typescript2.0,只是想知道这样的事情是否可能,如果可能,正确的语法是什么: export interface IElement {...} export interface X { one<T>(name: string, id: string): T extends IElement; two<T>(name: string, id: string): Observable<T extends IElement>; three<T&

只是想知道这样的事情是否可能,如果可能,正确的语法是什么:

 export interface IElement {...}

 export interface X {
     one<T>(name: string, id: string): T extends IElement;
     two<T>(name: string, id: string): Observable<T extends IElement>;
     three<T>(name: string, id: string): Observable<Array<T extends IElement>>;
 }
导出接口元素{…}
导出接口X{
一个(名称:string,id:string):T扩展元素;
二(名称:string,id:string):可观察;
三(名称:string,id:string):可观察;
}

基本上,我希望返回一个对象,该对象具有使用者在方法调用中指定的对象的所有属性,以及另一个接口的所有方法/道具。

这是可能的。只需将
扩展
约束放在通用签名中:

export interface IElement {}

export interface X {
    one<T extends IElement>(name: string, id: string): T;
    two<T extends IElement>(name: string, id: string): Observable<T>;
    three<T extends IElement>(name: string, id: string): Observable<Array<T>>;
}
导出接口元素{}
导出接口X{
一(名称:string,id:string):T;
二(名称:string,id:string):可观察;
三(名称:string,id:string):可观察;
}

这是可能的。只需将
扩展
约束放在通用签名中:

export interface IElement {}

export interface X {
    one<T extends IElement>(name: string, id: string): T;
    two<T extends IElement>(name: string, id: string): Observable<T>;
    three<T extends IElement>(name: string, id: string): Observable<Array<T>>;
}
导出接口元素{}
导出接口X{
一(名称:string,id:string):T;
二(名称:string,id:string):可观察;
三(名称:string,id:string):可观察;
}
交叉口类型

interface IElement { }

interface X {
    one<T>(name: string, id: string): T & IElement;
    two<T>(name: string, id: string): Observable<T & IElement>;
    three<T>(name: string, id: string): Observable<Array<T & IElement>>;
}
接口元素{}
接口X{
一个(名称:string,id:string):T&IElement;
二(名称:string,id:string):可观察;
三(名称:string,id:string):可观察;
}
交叉口类型

interface IElement { }

interface X {
    one<T>(name: string, id: string): T & IElement;
    two<T>(name: string, id: string): Observable<T & IElement>;
    three<T>(name: string, id: string): Observable<Array<T & IElement>>;
}
接口元素{}
接口X{
一个(名称:string,id:string):T&IElement;
二(名称:string,id:string):可观察;
三(名称:string,id:string):可观察;
}

这不是说消费者传递到函数中的t已经实现了IEElement吗?我的目的是将IEElement添加到T并返回它。对于此约束,要使T有效,需要实现IEElement接口。这不是你想要的吗?不是:给定对象X,我想返回对象X+添加的属性/方法(IEElement)并获取它们的intellisense。。。基本上我只想约束/定义返回类型,这不是说消费者传递到函数中的t已经实现了IElement吗?我的目的是将IEElement添加到T并返回它。对于此约束,要使T有效,需要实现IEElement接口。这不是你想要的吗?不是:给定对象X,我想返回对象X+添加的属性/方法(IEElement)并获取它们的intellisense。。。基本上,我只想约束/定义返回类型