Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Typescript Jasmine`TothRowerr`类型脚本定义(d.ts)似乎缺少第三个签名_Typescript_Jasmine - Fatal编程技术网

Typescript Jasmine`TothRowerr`类型脚本定义(d.ts)似乎缺少第三个签名

Typescript Jasmine`TothRowerr`类型脚本定义(d.ts)似乎缺少第三个签名,typescript,jasmine,Typescript,Jasmine,我已经使用jasmine/index.d.ts用Typescript编写了一些测试 有几个测试使用tothrower断言已抛出特定的自定义错误类 所有测试都通过,但TS编译器抛出以下错误: 错误TS2345:类型为“typeof MyCustomError”的参数不能分配给类型为“error”的参数。 我检查了jasmine文档,除非我有误解,否则第一个参数可能是错误类 expect(foo).tothrower(TypeError) 但是jasmine index.d.ts只列出了tothro

我已经使用
jasmine/index.d.ts
用Typescript编写了一些测试

有几个测试使用
tothrower
断言已抛出特定的自定义错误类

所有测试都通过,但TS编译器抛出以下错误:

错误TS2345:类型为“typeof MyCustomError”的参数不能分配给类型为“error”的参数。

我检查了jasmine文档,除非我有误解,否则第一个参数可能是错误类

expect(foo).tothrower(TypeError)

但是
jasmine index.d.ts
只列出了
tothrower
的两个签名:

tothrowerr(message?:string | RegExp):布尔值

tothrowerr(应为?:错误,消息?:字符串| RegExp):布尔值

我对TS的理解仍然有点不确定,但我想知道是否还应该有第三个签名,允许第一个参数是扩展错误的类,而不是实例?比如:

TothRower(预期?:错误类型,消息?:字符串| RegExp):布尔值

当我尝试将其添加到
.d.ts
时,我会说“类似”,但随后出现以下错误:

错误TS2345:类型为“typeof MyCustomError”的参数不能分配给类型为“ErrorConstructor”的参数。


自定义错误类

class MyCustomError extends Error {

    constructor() {
        super('A custom error message.');
    }
}
测试逻辑

it('should throw an error', () => {

    var obj = new MyObject();

    expect(() => obj.doSomething()
        .toThrowError(MyCustomError);
});

防止TS错误的当前解决方案

如果我将该错误作为实例,则TS错误停止

it('should throw an error', () => {

    var obj = new MyObject();

    expect(() => obj.doSomething()
        .toThrowError(new MyCustomError());
});

更新

it('should throw an error', () => {

    var obj = new MyObject();

    expect(() => obj.doSomething()
        .toThrowError(TypeError);
});
因此,我发现如果我将测试中的错误类型更改为
TypeError
MyIDE(Webstorm),那么TS编译器都会抱怨(并且我的测试正确地失败了):

参数类型TypeErrorConstructor不能分配给参数类型Error

但是,如果我随后将前面的第3个签名重新添加到
jasmine/index.d.ts
,那么IDE和ts编译器都会很高兴

TothRower(预期?:错误类型,消息?:字符串| RegExp):布尔值


这表明
d.ts
缺少签名,并且我的自定义错误类声明不正确?

这似乎是tsd中的一个错误

Jasmine当然没有表示您可以将
错误的实例传递给
TothRower()
。签名应更改为
typeof Error


您可以在DefinitelyTyped上提交一个问题,但是如果您想更快地修复它,最好使用原始Jasmine示例作为测试代码提交一个pull请求。

谢谢,看起来这个问题已经在最新的Jasmine.d.ts-
TothRower中修复(预期?:new(…args:any[])=>错误,消息?:string | RegExp):布尔