Angular 单元测试类型脚本类型
我的一个模块中有以下导出:Angular 单元测试类型脚本类型,angular,typescript,jasmine,typescript-typings,Angular,Typescript,Jasmine,Typescript Typings,我的一个模块中有以下导出: export class Action1 implements Action {} export class Action2 implements Action {} export type ActionsUnion = | Action1 | Action2; 我正在努力找出测试ActionsUnion的最佳方法,以确保它属于我定义的类型。例如: it('should have the correct types', () => { expe
export class Action1 implements Action {}
export class Action2 implements Action {}
export type ActionsUnion =
| Action1
| Action2;
我正在努力找出测试ActionsUnion
的最佳方法,以确保它属于我定义的类型。例如:
it('should have the correct types', () => {
expect(typeof Action1).toEqual(ActionsUnion);
expect(typeof Action2).toEqual(ActionsUnion);
});
当然,由于我使用ActionsUnion
作为变量,上述方法不起作用。关于如何实现上述目标有何想法
在上下文中,我使用angular、ngrx和jasmine。我还没有尝试使用union,但您可能想尝试jasmine.any(类型)。在这种情况下,上面的代码是:
it('should have the correct types', () => {
expect(Action1).toEqual(jasmine.any(ActionsUnion));
expect(Action2).toEqual(jasmine.any(ActionsUnion));
});
详情如下:
你想实现什么目标?是否有任何检查不是由编译器执行的?我们使用
dtslint
来测试RxJS的类型声明:您还可以看看-我最初计划使用它来测试RxJS的类型,但是dtslint
更适合编写大量期望值。@Cerberus我想做一个测试,确保ActionsUnion
类型包含所有相应的类类型@卡坦特-谢谢你的建议,我会看看相关的:(包括讨论)谢谢你的建议。我得到一个TS Lint错误,错误TS2693:“ActionsUnion”只引用一个类型,但在这里被用作一个值。
然后在执行时,引用错误:ActionsUnion未定义
这是完全错误的。你把类型和价值搞乱了。