Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.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
Javascript 正在进行的感叹号对JSDoc@type标记有什么作用?_Javascript_Visual Studio Code_Google Closure Compiler_Jsdoc - Fatal编程技术网

Javascript 正在进行的感叹号对JSDoc@type标记有什么作用?

Javascript 正在进行的感叹号对JSDoc@type标记有什么作用?,javascript,visual-studio-code,google-closure-compiler,jsdoc,Javascript,Visual Studio Code,Google Closure Compiler,Jsdoc,VS代码解析以下内容时,就像没有感叹号一样,没有任何错误: const el = /** @type {HTMLElement!} */ (document.getElementById("abc")); 它有什么作用?JSDoc的官方文件说: 指示该值为指定类型,但不能为null 不确定进程标记的作用。TLDR:!HTMLElement和HTMLElement是相同的注释 这是一个类型转换。它表示document.getElementById(“abc”)是一个HtmleElement,而

VS代码解析以下内容时,就像没有感叹号一样,没有任何错误:

const el = /** @type {HTMLElement!} */ (document.getElementById("abc"));
它有什么作用?JSDoc的官方文件说:

指示该值为指定类型,但不能为null

不确定进程标记的作用。

TLDR:
!HTMLElement
HTMLElement是相同的注释


这是一个类型转换。它表示
document.getElementById(“abc”)
是一个HtmleElement,而不是null。添加注释可能是因为返回HtmleElement或null

这一行的作者应该100%确信id为
abc
的元素存在,因为在进行此转换后,闭包编译器将把此类型视为
HTMLElement
,而不是
HTMLElement
null

转换中使用的类型与转换之外的类型遵循相同的规则,因此为了直接演示如何使用这些类型,下面的示例不使用转换

正如OP所说,在闭包编译器中,感叹号意味着类型必须存在,并且不能是
null
。此外,问号表示它可以是类型,也可以是
null

以以下代码为例:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print
// ==/ClosureCompiler==

const body = document.querySelector('body');

const /** @export {Element!} */ n1 = body;
const /** @export {!Element} */ n2 = body;
const /** @export {Element} */ n3 = null;
const /** @export {?Element} */ n4 = null;
const /** @export {Element?} */ n5 = null;

console.log({
  n1,
  n2,
  n3,
  n4,
})
以下是编译器生成的警告:

JSC_TYPE_MISMATCH: initializing variable
found   : (Element|null)
required: Element at line 3 character 37
const /** @export {Element!} */ n1 = body;
                                     ^
JSC_TYPE_MISMATCH: initializing variable
found   : (Element|null)
required: Element at line 4 character 37
const /** @export {!Element} */ n2 = body;
                                     ^

请注意,
body
document.querySelector
返回,其类型为
{Element}
,我们已经说过编译器可以理解为
元素
,也称为
元素|空
。此示例显示,符号也可以表示为
?Element
Element?

噢,所以
!HTMLElement
HTMLElement完全相同!在类型表示法之后使用它对我来说更有意义,因为在常规JS中,在前面使用感叹号可以标记相反的内容<代码>(!true==false)
等于:
true
。另一点:从语法的角度来看,当你在类型定义后添加问号时,你会怀疑它是否正确。雷米同意,以我的经验,
运算符实际上总是在类型之前。不过,我认为最好重述一下您的示例:
!(真==假)
JSC_TYPE_MISMATCH: initializing variable
found   : (Element|null)
required: Element at line 3 character 37
const /** @export {Element!} */ n1 = body;
                                     ^
JSC_TYPE_MISMATCH: initializing variable
found   : (Element|null)
required: Element at line 4 character 37
const /** @export {!Element} */ n2 = body;
                                     ^