警告TS0:@param上的类型注释与其TypeScript类型无关,请删除{...}部分

时间:2018-07-17 11:22:48

标签: angular typescript

编译我的角度应用程序时出现错误:

  

错误: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);
}

如果删除注释部分的@param,该错误消失了,但是在编写代码并调用此函数时,我也会失去其他信息。

有人知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:4)

解决方案是从注释中删除{},错误不再抛出:

/**
 * @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)
 */

它表明您不需要在JsDoc上的TypeScript上指定字段的类型(我在here中找到了该信息)。

之所以会这样,是因为JsDocs会解释代码并已经知道您在函数的参数上声明的类型,因此,您不再需要在注释中声明它们。

答案 1 :(得分:4)

tsconfig.json 文件中有可用于忽略这些注释警告的配置标志。我将下面的标志设置为 false ,它停止吐出这些警告。

"angularCompilerOptions": {
  "annotateForClosureCompiler": false
}

答案 2 :(得分:0)

仅调整@Ricardo Rocha的答案。

我没有将专用类型添加到@params中,并且还收到了上述错误消息。添加特定类型后,tsLint错误消失了。

 /**
   * @description This method will try to load the image from appSettings.
   *
   * @param string billUUID
   * @param documentUUID documentUUID
   * @param string resultRef
   */