Lodash _.memoize"参数类型与参数不匹配"

时间:2016-08-09 14:59:26

标签: typescript lodash

出于某种原因,我似乎无法让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;
    }
}

1 个答案:

答案 0 :(得分:0)

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