我正在使用现有的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
不存在。谁能解释我为什么收到此消息?
Typescript版本3.7.5
PS:我将打字稿包更新为最新版本3.8.3,结果相同。
我发现了问题,这是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.onFold
更改为options.onOverflow
。