Typescript会抛出Array.find错误

时间:2016-03-16 02:07:58

标签: typescript angular ecmascript-6

我刚刚完成了Angular 2.0英雄之旅教程,并且我已经添加了以下Gulp文件(本例简化)来构建它:

var del = require('del');
var gulp = require('gulp');
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var tsProject = ts.createProject('tsconfig.json');

gulp.task('transpile-ts', function() {
    var tsResult = gulp.src(paths.allTypeScript)
        .pipe(sourcemaps.init())
        .pipe(ts(tsProject));

    return tsResult.js
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(paths.dest_js));
});

我想将一个Array.find方法添加到dashboard.component.ts中,如下所示:

ngOnInit() {
    let newVar: Array<number> = new Array();
    newVar.push(0);
    newVar.push(1);
    newVar.find(d => d == 1);

    this._heroService.getHeroes()
        .then(heroes => this.heroes = heroes.slice(1,5));
}

当我运行命令&#34; gulp transpile-ts&#34;但是我收到以下错误:

app \ dashboard.component.ts(26,16):错误TS2339:属性&#39;找到&#39;不存在 在类型&#39;数字[]&#39;。

我有es6-shim.d.ts,所以&#34;发现&#34;方法确实存在于&#34;接口数组&#34;。

此外,我尝试使用Grunt运行相同的任务,并且发生了同样的问题,因此它不是Gulp问题。

关于可能导致这种情况的任何想法?

2 个答案:

答案 0 :(得分:5)

  

我有es6-shim.d.ts,所以&#34;发现&#34;方法确实存在于&#34;接口数组&#34;。

之下

显然,shim没有将find方法添加到Array。你应该:

interface Array<T> {
    /** 
      * Returns the value of the first element in the array where predicate is true, and undefined 
      * otherwise.
      * @param predicate find calls predicate once for each element of the array, in ascending 
      * order, until it finds one where predicate returns true. If such an element is found, find 
      * immediately returns that element value. Otherwise, find returns undefined.
      * @param thisArg If provided, it will be used as the this value for each invocation of 
      * predicate. If it is not provided, undefined is used instead.
      */
    find(predicate: (value: T, index: number, obj: Array<T>) => boolean, thisArg?: any): T;
}

更新

即使多个声明也不应该导致错误。以下编译就好了:

interface Array<T> {
    /** 
      * Returns the value of the first element in the array where predicate is true, and undefined 
      * otherwise.
      * @param predicate find calls predicate once for each element of the array, in ascending 
      * order, until it finds one where predicate returns true. If such an element is found, find 
      * immediately returns that element value. Otherwise, find returns undefined.
      * @param thisArg If provided, it will be used as the this value for each invocation of 
      * predicate. If it is not provided, undefined is used instead.
      */
    find(predicate: (value: T, index: number, obj: Array<T>) => boolean, thisArg?: any): T;    

}
interface Array<T> {
    /** 
      * Returns the value of the first element in the array where predicate is true, and undefined 
      * otherwise.
      * @param predicate find calls predicate once for each element of the array, in ascending 
      * order, until it finds one where predicate returns true. If such an element is found, find 
      * immediately returns that element value. Otherwise, find returns undefined.
      * @param thisArg If provided, it will be used as the this value for each invocation of 
      * predicate. If it is not provided, undefined is used instead.
      */
    find(predicate: (value: T, index: number, obj: Array<T>) => boolean, thisArg?: any): T;    
}

var foo:any[]
foo.find((x)=>true);

请检查es6-shim.d.ts的内容以确保确认。

答案 1 :(得分:1)

经过调查,我注意到使用&#39; tsc&#39;在CMD行中传递Typsecript一切按预期工作。而不是使用&#39; gulp.src&#39;在我的gulp文件中,我应该使用&#39; tsProject.src&#39;模仿这个。

这里的回答是https://github.com/ivogabe/gulp-typescript/issues/313