Typescript-具有数据模型接口的多态性
我想我在这里寻找一种设计模式。这是我所拥有的- IApp.ts IAPPP.ts IAndroidApp.ts 表示传入数据的非常简单的模型,没有什么特别之处 现在我有了一个服务,它可以进行API调用并以IApp的形式获取数据 apps.service.ts 然后我在Apple组件中使用appsService.appiOSVersion,在Android组件中使用appsService.appAndroidVersion。苹果和安卓组件都有不同的逻辑。我试图在每个组件中使用appsService.appVersion,但由于它是基本IApp类型,因此该对象没有特定于平台的属性;所以必须使用两个不同的变量 我的问题:苹果和安卓组件中是否都可以重用appsService.appVersion?我需要此变量位于apps.service.ts中,用于状态管理。拥有两个不同的变量并不疯狂,对吧 如果这不合理,请道歉。如果你需要更多的细节,请告诉我Typescript-具有数据模型接口的多态性,typescript,interface,polymorphism,Typescript,Interface,Polymorphism,我想我在这里寻找一种设计模式。这是我所拥有的- IApp.ts IAPPP.ts IAndroidApp.ts 表示传入数据的非常简单的模型,没有什么特别之处 现在我有了一个服务,它可以进行API调用并以IApp的形式获取数据 apps.service.ts 然后我在Apple组件中使用appsService.appiOSVersion,在Android组件中使用appsService.appAndroidVersion。苹果和安卓组件都有不同的逻辑。我试图在每个组件中使用appsService
非常感谢 在什么时候设置this.appVersion.platform变量?您能否使用类似appsService.fetch的泛型参数化appService的fetch调用,并且在调用它时,使用特定于平台的接口参数化它 如果你这样做: appService.fetch。。。
您可以让返回类型为泛型T,并访问该接口的结构。泛型!我在我的C API中使用了大量这样的代码,但在TypeScript中我并没有想到。非常感谢你的提示:最近开始玩Angular
export interface IApp {
platform: PlatformEnum;
version: string;
islive: boolean;
title: string;
iconurl: string;
}
export interface IAppleApp extends IApp {
buildversion: string;
versionstatus: string;
sku: boolean;
category: string;
accountid: string;
}
export interface IAndroidApp extends IApp {
versioncodes: string;
track: string;
rolloutpercentage: boolean;
acccountname: string;
}
appVersion: IApp;
appiOSVersion: IAppleApp;
appAndroidVersion: IAndroidApp;
if (this.appVersion.platform === PlatformEnum.ios) {
this.appiOSVersion = this.appVersion as IAppleApp;
} else if (this.appVersion.platorm === PlatformEnum.android) {
this.appAndroidVersion = this.appVersion as IAndroidApp;
}