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