Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
Angular 单元测试类型脚本类型_Angular_Typescript_Jasmine_Typescript Typings - Fatal编程技术网

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未定义
这是完全错误的。你把类型和价值搞乱了。