Ric*_*cha 4 typescript angular
编译我的角度应用程序时出现错误:
错误:ngc编译失败:ng-formly / core / src / utils.ts(194,1):警告TS0:@param上的类型注释因其TypeScript类型而多余,请删除{...}部分
在utils.ts文件中,我具有以下功能:
/**
* Delegates the subscription of any event and assign the subscription to a particulary domElement.
* Each Time the event is trigger, the handler function will be call
* Ex: delegate("domElement", "focus", "input", (focus) => console.log(focus))
* @param {any} domElement The dom element. Ex: document
* @param {string} eventType The event type. Ex."focus"
* @param {string} domElementType The dom element type. Ex. "input"
* @param {Function} author The handler function. Ex. (focus) => console.log(focus)
*/
export function delegate(domElement: any, eventType: string, domElementType: string, handler): void {
domElement.eventListenerHandler = domElement.eventListenerHandler || {};
if(domElement.eventListenerHandler[domElementType])
domElement.removeEventListener(domElementType, domElement.eventListenerHandler[domElementType], eventType === "focus" ? true : false);
domElement.eventListenerHandler[domElementType] = (event) => {
var t = event.target;
while (t && t !== this) {
if (t.matches && t.matches(domElementType)) {
handler.call(t, event);
}
t = t.parentNode;
}
}
domElement.addEventListener(eventType, domElement.eventListenerHandler[domElementType], eventType === "focus" ? true : false);
}
Run Code Online (Sandbox Code Playgroud)
如果删除注释部分的@param,该错误就会消失,但是在编写代码并调用此函数时,我也会失去其他信息。
有谁知道如何解决这个问题?
Ric*_*cha 11
解决的办法是{}从注释中删除,错误不再抛出:
/**
* @param domElement The dom element. Ex: document
* @param eventType The event type. Ex."focus"
* @param domElementType The dom element type. Ex. "input"
* @param author The handler function. Ex. (focus) => console.log(focus)
*/
Run Code Online (Sandbox Code Playgroud)
它表明您不需要在JsDoc的TypeScript上指定字段的类型(我在此处找到了该信息)。
发生这种情况是因为JsDocs会解释代码,并且已经知道您在函数的参数上声明的类型,因此,您不再需要在注释中声明它们。
tsconfig.json文件中有可用于忽略这些注释警告的配置标志。我将下面的标志设置为false,它停止吐出这些警告。
"angularCompilerOptions": {
"annotateForClosureCompiler": false
}
Run Code Online (Sandbox Code Playgroud)