Angularjs 打字脚本+;角度1+;筛选器:分配类型

Angularjs 打字脚本+;角度1+;筛选器:分配类型,angularjs,typescript,Angularjs,Typescript,我的TypeScript 1.5+AngularJS 1项目中有以下过滤器: module Filters{ export function percentage($filter:angular.IFilterService) { return function(input: number, decimals: number): string{ return $filter('number')(input * 100, decimals) + '

我的TypeScript 1.5+AngularJS 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[];
}
接口过滤服务{
/**
*用法:
*$filter(名称);
*
*@param name要检索的筛选函数的名称
*/
(名称:字符串):IFilterFunc;
}
接口IFilterFunc{
(数组:T[],表达式:字符串| IFilterPatternObject | IFilterPredicateFunc,comparator?:IFilterComparatorFunc | boolean):T[];
}

然而,我不确定问题出在哪里。有什么想法吗

不确定您使用的打字版本,但在最新的1.4+打字版本中,过滤器打字的定义已经更新

因此,在重载的帮助下专门处理传入的筛选器类型,如下所示:

 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;
    }
接口过滤服务{
(名称:“过滤器”):IFilterFilter;
(名称:“货币”):IFilterCurrency;
(名称:“编号”):IFilterNumber;
(名称:“日期”):IFilterDate;
(名称:“json”):IFilterJson;
(名称:“lowercase”):IFilterLowercase;
(名称:“大写”):IFilterUppercase;
(名称:“Limito”):IFilterLimito;
(名称:“orderBy”):IFilterOrderBy;
/**
*用法:
*$filter(名称);
*
*@param name要检索的筛选函数的名称
*/
(名称:字符串):T;
}
接口IFilterNumber{
/**
*将数字格式化为文本。
*@param number要格式化的数字。
*@param fractionSize小数位数,将数字四舍五入。如果未提供此项,则分数大小将根据当前区域设置的数字格式模式计算。对于默认区域设置,分数大小将为3。
*@返回数字四舍五入到小数点,并在每三位数后加“,”。
*/
(值:数字|字符串,fractionSize?:数字|字符串):字符串;
}

将angular.d.ts向上渐变可以解决您的问题。看起来您有一个版本的过滤器类型是错误的。

也许这会有所帮助。然而,我无法重现你的问题。谢谢!我一回到家就给它拍一张:)