出于某种原因,我似乎无法得到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;
}
}
您可以安装模块声明文件:typings install --save lodash
。如果在文件的顶部显式地使用import * as _ from 'lodash'
,而不是var _: _.LodashStatic
,那么一切都应该没问题。