【问题标题】:Destructuring argument: TS2339 Property does not exist解构参数:TS2339 属性不存在
【发布时间】:2020-03-29 06:29:02
【问题描述】:

我正在使用现有的 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:我将 typescript 包更新到最新版本 3.8.3,结果相同。

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-14
      • 2017-02-25
      • 2019-11-10
      • 2020-07-31
      • 1970-01-01
      • 2021-11-24
      • 2021-07-04
      • 2019-01-11
      相关资源
      最近更新 更多