2016-08-09 14 views
0

Aus irgendeinem Grund kann ich nicht scheinen, die IDE (IntelliJ) zu bekommen, um sich nicht darüber zu beschweren, irgendeine Idee, was das Problem ist? Ich dachte memoize eine Funktion als Parameter übernehmen könnte:Lodash _.memoize "Argumenttypen stimmen nicht mit Parametern überein"

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; 
    } 

/* Nachtrag */

Die LodashStatic hat folgende Informationen in der Definitionsdatei

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; 
    } 
} 
+0

Verwenden Sie eine aktuelle lodash.d.ts, und referenzieren (entweder durch '///

+0

Danke für @MikeMcCaughan, ich habe den Code in der Frage geändert. Ich verwende 'declare var _: _.LoDashStatic;' – ThinkBonobo

+0

Auch zu beachten, bekomme ich solche Fehler nicht, wenn ich '_.once' anstelle von' _.memoize' verwende, aber in meiner Funktion will ich nicht einmal benutzen. – ThinkBonobo

Antwort

0

Anstatt sich auf Bei einer globalen Deklaration können Sie immer die Moduldeklarationsdatei installieren: typings install --save lodash. Wenn Sie explizit import * as _ from 'lodash' an der Spitze Ihrer Datei anstelle von var _: _.LodashStatic, sollte alles in Ordnung sein.

Verwandte Themen