【问题标题】:Type assertion using the '<>' is forbidden,use 'as' instead?禁止使用“<>”进行类型断言,改用“as”?
【发布时间】:2019-12-26 04:57:39
【问题描述】:

我的规范中有以下测试用例,

it (should create,()=>{
     const url  = (<jasmine.spy> http.get).calls.args(0)[0]
     expect(url).toBe('/api/get')
});  

当我运行它时,我收到以下 lint 错误。

Type assertion using the '<>' is forbidden, use as instead?

任何人都可以提供帮助。

【问题讨论】:

  • http.get as jasmine.spy 是它想要你做的——就去做吧?

标签: angular typescript jasmine karma-jasmine angular-unit-test


【解决方案1】:

来自Type Assertion

类型断言

as foo&lt;foo&gt;

最初添加的语法是&lt;foo&gt;。如下所示:

var foo: any;
var bar = <string> foo; // bar is now of type "string"

但是,在 JSX 中使用 &lt;foo&gt; 样式断言时,语言语法存在歧义:

var foo = <string>bar;
</string>

因此,现在建议您只使用as foo 以保持一致性。

所以 linter 会警告旧语法。

所以在这种情况下,这是新语法:

const url  = (http.get as jasmine.spy).calls.args(0)[0]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
相关资源
最近更新 更多