2015-11-03 56 views
2

我在我的打字稿以下过滤器1.5 + AngularJS 1项目:打字稿+角1 +过滤器:指定类型

module Filters{ 

    export function percentage($filter:angular.IFilterService) { 

     return function(input: number, decimals: number): string{ 
      return $filter('number')(input * 100, decimals) + '%'; 
     } 

    } 


    var app = AppModule.getModule(); 
    app.filter("percentage", Filters.percentage); 
} 

它想改变输入的百分比值 - 我知道,我能做的它有不同的方式,但它不是关于过滤器本身 - 它将过滤器的类型分配给过滤器。

用下面的代码,我在编译过程中出现以下错误:

error TS2345: Argument of type 'number' is not assignable to parameter of type '{}[]'. 
    Property 'length' is missing in type 'Number'. 

指向我这个在分型:

interface IFilterService { 
    /** 
    * Usage: 
    * $filter(name); 
    * 
    * @param name Name of the filter function to retrieve 
    */ 
    (name: string): IFilterFunc; 
} 

interface IFilterFunc { 
    <T>(array: T[], expression: string | IFilterPatternObject | IFilterPredicateFunc<T>, comparator?: IFilterComparatorFunc<T>|boolean): T[]; 
} 

不过,我安静不知道什么是问题。有任何想法吗?

+0

也许这将有助于http://stackoverflow.com/a/33064403/99256。但是,我无法以某种方式重现您的问题。 –

回答

2

不确定您使用的是什么版本的打字,但自最新的1.4+打字版本以来,过滤打字的定义已更新。

所以definition for filter专门处理您通过在重载的帮助下过滤器类型,它看起来像这样:

interface IFilterService { 
    (name: 'filter'): IFilterFilter; 
    (name: 'currency'): IFilterCurrency; 
    (name: 'number'): IFilterNumber; 
    (name: 'date'): IFilterDate; 
    (name: 'json'): IFilterJson; 
    (name: 'lowercase'): IFilterLowercase; 
    (name: 'uppercase'): IFilterUppercase; 
    (name: 'limitTo'): IFilterLimitTo; 
    (name: 'orderBy'): IFilterOrderBy; 
    /** 
    * Usage: 
    * $filter(name); 
    * 
    * @param name Name of the filter function to retrieve 
    */ 
    <T>(name: string): T; 
} 

interface IFilterNumber { 
     /** 
     * Formats a number as text. 
     * @param number Number to format. 
     * @param fractionSize Number of decimal places to round the number to. If this is not provided then the fraction size is computed from the current locale's number formatting pattern. In the case of the default locale, it will be 3. 
     * @return Number rounded to decimalPlaces and places a “,” after each third digit. 
     */ 
     (value: number|string, fractionSize?: number|string): string; 
    } 

Updgrading的angular.d.ts应该解决您的问题。看起来像你有一个版本,有buggy过滤器类型是fixed during this merge

+0

谢谢!一旦我回家,我会给它一个拍摄:) – uksz

+0

@uksz当然,欢迎您。 :) – PSL

+1

就是这样:)谢谢! – uksz