typescript - 解构参数: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,结果相同。
解决方案
我发现了问题,是 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
。
推荐阅读
- tfs - Azure DevOps Scrum 项目冲刺工作流程未更新
- c# - 在制作按钮之前它无法正常工作
- node.js - 无法为不可为空的字段 Mutation.createUser 返回 null - Apollo Server
- javascript -
- python-3.x - 有没有办法在 Python unittest 中向模拟对象添加属性
- html - 引导卡列填充/边距溢出
- monetdb - MonetDB 的良好用例 - 具有大约 10-15 维和父/子层次结构的星型模式
- git - 如何在不解决当前分支冲突的情况下切换到分支?
- c - C中的复合文字存储持续时间
- python - 给定一个字典和一个键,“get_nth_element_of_value”返回位于给定键的列表的第 n 个元素