简体   繁体   English

禁止使用“<>”进行类型断言,请改用“as”?

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

I have the following test case in my spec,我的规范中有以下测试用例,

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

When I run it, I am getting the following lint error.当我运行它时,我收到以下 lint 错误。

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

Can anyone please suggest help.任何人都可以请提供帮助。

From Type Assertion .类型断言

Type assertions类型断言

as foo vs. <foo> as foo<foo>

Originally the syntax that was added was <foo> .最初添加的语法是<foo> This is demonstrated below:这在下面演示:

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

However, there is an ambiguity in the language grammar when using <foo> style assertions in JSX:但是,在 JSX 中使用<foo>样式断言时,语言语法存在歧义:

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

Therefore it is now recommended that you just use as foo for consistency.因此,现在建议您只使用as foo以保持一致性。

So the linter warns about the old syntax.所以 linter 会警告旧语法。

So in this case, this is in the new syntax:所以在这种情况下,这是在新语法中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angular和rxjs映射函数中使用类型断言 - Use type assertion in Angular and rxjs map function 如何在@input() 变量上使用类型断言? - How to use type assertion on @input() variable? 使用 HttpClient 在 Http 请求中键入断言 - Type Assertion in Http Requests with HttpClient 为什么我应该使用谓词作为返回类型而不是布尔值? - Why should i use a predicate as a return type instead of a boolean? HTTP:从 JSON 对象到 Typescript 的类型断言 - HTTP: Type assertion from JSON object to Typescript 对象和原始值之间的类型声明失败 - Type Assertion failing between Object and primitive value 重构 Observable 以避免类型断言错误 - Refactor Observable to avoid type assertion errors “对象”类型可分配给极少数其他类型。 您是不是要改用“任何”类型 - The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead “对象”类型可分配给极少数其他类型。 您的意思是改用“任何”类型吗? - The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? “对象”类型可分配给很少的其他类型。 您是想改用“any”类型吗? 类型“对象”缺少以下属性 - The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properti
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM