打字稿:拆分一个大界面
Typescript: Split a big interface
我已经创建了一个接口来接收来自 HTTP 请求的响应。服务的响应有很多元素,所以我的界面很大。
界面
export interface LargeInterface {
object_1: string;
object_2: string;
object_3: string;
object_4: string;
object_5: string;
object_6: string;
...
object_n: string;
}
通话
this.http.get<LargeInterface>( this.url, formData );
我想将界面拆分成多个部分,但我需要 get 方法正确解析它。哪种方法比较好?
我尝试过继承,但我认为这不是一个好的做法,因为界面各部分之间的关系不是基于继承。
提前致谢。
您可以将接口与 &
组合
interface I1 {
object_1: string
}
interface I2 {
object_2: string
}
type LargeInterface = I1 & I2
您可以创建子界面并将它们合并为一个
interface Base_Data {
object_1: string
}
interface Special_Data {
object_2: string
}
interface LargeInterface extends Base_Data, Special_Data {
}
并按预期使用它们
this.http.get<LargeInterface>( this.url, formData );
我已经创建了一个接口来接收来自 HTTP 请求的响应。服务的响应有很多元素,所以我的界面很大。
界面
export interface LargeInterface {
object_1: string;
object_2: string;
object_3: string;
object_4: string;
object_5: string;
object_6: string;
...
object_n: string;
}
通话
this.http.get<LargeInterface>( this.url, formData );
我想将界面拆分成多个部分,但我需要 get 方法正确解析它。哪种方法比较好?
我尝试过继承,但我认为这不是一个好的做法,因为界面各部分之间的关系不是基于继承。
提前致谢。
您可以将接口与 &
interface I1 {
object_1: string
}
interface I2 {
object_2: string
}
type LargeInterface = I1 & I2
您可以创建子界面并将它们合并为一个
interface Base_Data {
object_1: string
}
interface Special_Data {
object_2: string
}
interface LargeInterface extends Base_Data, Special_Data {
}
并按预期使用它们
this.http.get<LargeInterface>( this.url, formData );