首页 > 解决方案 > 解构参数:TS2339 属性不存在

问题描述

我正在使用现有的 javascript 函数。我开始使用 --checkJs 选项,使用打字稿检查代码,即使它位于 .js 文件中。该函数对其最后一个参数使用解构,这似乎很重要,而且令人困惑......

export function foldFlowLines(
  text, indent, mode,
  { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow }
  ) {
   // ...body containing...

   if (overflow && onOverflow) onOverflow()
   if (folds.length === 0) return text
   if (onFold) onFold()

   // ...
}

我收到以下消息:

src/foldFlowLines.js:42:66 - error TS2339: Property 'onOverflow' does not exist on type 
'{ indentAtStart?: number; lineWidth?: number; minContentWidth?: number; onFold: Function; }'.

42   { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow }
                                                                    ^^^^^^^^^^

tsc 似乎以某种方式推断出这onFold是一个功能,这很好,但不知何故它抱怨onOverflow不存在。谁能解释我为什么收到这条消息?

打字稿版本 3.7.5

PS:我将 typescript 包更新到最新版本 3.8.3,结果相同。

标签: typescripttsc

解决方案


我发现了问题,是 JSDoc 注释中的错误!

这是原始的,有细微的错误:

/**
 * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
 * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
 * terminated with `\n` and started with `indent`.
 *
 * @param {string} text
 * @param {string} indent
 * @param {string} [mode='flow'] `'block'` prevents more-indented lines
 *   from being folded; `'quoted'` allows for `\` escapes, including escaped
 *   newlines
 * @param {Object} options
 * @param {number} [options.indentAtStart] Accounts for leading contents on
 *   the first line, defaulting to `indent.length`
 * @param {number} [options.lineWidth=80]
 * @param {number} [options.minContentWidth=20] Allow highly indented lines to
 *   stretch the line width
 * @param {function} options.onFold Called once if the text is folded
 * @param {function} options.onFold Called once if any line of text exceeds
 *   lineWidth characters
 */
export function foldFlowLines(
  text,
  indent,
  mode,
  { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow }
) { 
//... 
}

最终@param需要从 更改options.onFoldoptions.onOverflow


推荐阅读