【问题标题】:Lodash _.memoize "Argument types do not match parameters"Lodash _.memoize “参数类型与参数不匹配”
【发布时间】:2016-08-09 14:59:26
【问题描述】:

由于某种原因,我似乎无法让 IDE (IntelliJ) 不抱怨这一点,知道问题出在哪里吗?我认为 memoize 可以将函数作为参数:

declare var _: _.LoDashStatic;

export class MemoizeService {
    'use strict';

    masterCatTree;
    ...

    flattenTree = _.memoize(this._flattenTree);
    private _flattenTree(tree?: any, level?: number) {
        let level;
        const self = this;
        if (!tree) {
            tree = self.masterCatTree;
            level = 0;
        }

        let arr = [];
        if (tree) {
            for (let key in tree) {
                if (!tree.hasOwnProperty(key)) {
                    continue;
                }
                let val = angular.copy(tree[key]);
                if (level) {
                    val.name = _.times(level, _.constant('-')).join('') + ' ' + val.name;
                }
                arr.push(val);
                arr.push(...self.flattenTree(val.children, level + 1));
            }
        }
        return arr;
    }

/* 附录 */

LodashStatic 在定义文件中有以下信息

interface LoDashStatic {
    /**
     * Creates a function that memoizes the result of func. If resolver is provided it determines the cache key for
     * storing the result based on the arguments provided to the memoized function. By default, the first argument
     * provided to the memoized function is coerced to a string and used as the cache key. The func is invoked with
     * the this binding of the memoized function.
     * @param func The function to have its output memoized.
     * @param resolver The function to resolve the cache key.
     * @return Returns the new memoizing function.
     */
    memoize: {
        <T extends Function>(func: T, resolver?: Function): T & MemoizedFunction;
        Cache: MapCache;
    }
}

【问题讨论】:

  • 您是否使用最新的 lodash.d.ts,并引用它(通过/// &lt;referenceimport)?
  • 感谢您关注@MikeMcCaughan,我修改了问题中的代码。我正在使用declare var _: _.LoDashStatic;
  • 还要注意,当我使用 _.once 而不是 _.memoize 但在我的函数中我不想使用一次时,我不会收到此类错误。
  • 是的,我会尝试找到定义 memoize 的 .d.ts 文件。我认为 IntelliJ 有能力“去定义”一个函数吗?然后你可以看看它是否将一个函数作为参数。
  • 我在定义文件中添加了以下信息。我真的看不出它有什么问题,但我真的不习惯查看定义文件。你看到什么了吗?

标签: typescript lodash


【解决方案1】:

您始终可以安装模块声明文件,而不是依赖全局声明:typings install --save lodash。如果您在文件顶部明确import * as _ from 'lodash',而不是var _: _.LodashStatic,一切都应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多