Angular 如何检查结果类型
我有一个非常简单的接口类型,如下所示Angular 如何检查结果类型,angular,typescript,unit-testing,Angular,Typescript,Unit Testing,我有一个非常简单的接口类型,如下所示 export interface Amount { totalAmount: number; } 在我的单元测试中,我想检查从API调用返回的对象是否属于这种类型,但我不知道该怎么做。我的API调用如下: const expectedResponse = { totalAmount: 5000 }; amountDataService .getAmountData(params) .subscribe( r
export interface Amount {
totalAmount: number;
}
在我的单元测试中,我想检查从API调用返回的对象是否属于这种类型,但我不知道该怎么做。我的API调用如下:
const expectedResponse = {
totalAmount: 5000
};
amountDataService
.getAmountData(params)
.subscribe(
result => {
expect(result instanceof Amount).toBe(true);
expect(result).toEqual(expectedResponse);
},
error => {
fail('Data was not returned successfully.');
}
);
但是,行expect(金额的结果实例).toBe(true)代码>显示错误消息:
'Amount' only refers to a type, but is being used as a value here
如何检查返回对象的类型?Dup of
无法在运行时检查接口,因为类型信息不会以任何方式转换为已编译的JavaScript代码
您可以检查特定的属性或方法,并决定要执行的操作
module MyModule {
export interface IMyInterface {
name: string;
age: number;
}
export interface IMyInterfaceA extends IMyInterface {
isWindowsUser: boolean;
}
export interface IMyInterfaceB extends IMyInterface {
}
export function doSomething(myValue: IMyInterface){
// check for property
if (myValue.hasOwnProperty('isWindowsUser')) {
// do something cool
}
}
}
这回答了你的问题吗?