Typescript会抛出Array.find错误

Chr*_*igg 1 typescript ecmascript-6 angular

我刚刚完成了Angular 2.0 Tour of Heroes教程,我添加了以下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));
});
Run Code Online (Sandbox Code Playgroud)

我想将一个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));
}
Run Code Online (Sandbox Code Playgroud)

当我运行命令"gulp transpile-ts"但是我收到以下错误:

app\dashboard.component.ts(26,16):错误TS2339:类型'number []'上不存在属性'find'.

我有es6-shim.d.ts,因此"find"方法确实存在于"interface Array"下.

此外,我尝试与Grunt运行相同的任务,同样的问题发生,所以这不是一个Gulp问题.

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

bas*_*rat 5

我有es6-shim.d.ts,因此"find"方法确实存在于"interface Array"下.

显然,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;
}
Run Code Online (Sandbox Code Playgroud)

更新

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

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);
Run Code Online (Sandbox Code Playgroud)

所以检查es6-shim.d.ts的内容以确保它确认.