【问题标题】:Typescript throws error with Array.findTypescript 使用 Array.find 引发错误
【发布时间】:2016-03-16 02:07:58
【问题描述】:

我刚刚完成了 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));
});

我想在dashboard.component.ts中添加一个Array.find方法如下:

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

当我运行命令“gulp transpile-ts”时,我收到以下错误:

app\dashboard.component.ts(26,16):错误 TS2339:属性“查找”不存在 在类型“数字[]”上。

我包含了 es6-shim.d.ts,因此“find”方法确实存在于“interface Array”下。

我还尝试使用 Grunt 运行相同的任务,但发生了相同的问题,所以这不是 Gulp 问题。

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

【问题讨论】:

    标签: typescript angular ecmascript-6


    【解决方案1】:

    我已经包含了 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;
    }
    

    更新

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

    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 的内容以确保它确认。

    【讨论】:

    • 感谢您的回复 basarat。我在 es6-shim 文件中确实有该代码,我什至可以导航到它。当我在“查找”属性上“选择声明”时,我确实有多种选择,其中包含诸如“global.d.ts”之类的文件,不确定这是否与它有关。
    • 多个声明不应导致错误。查看更新。
    • 我发现问题出在“gulp-typescript”上。当我只使用 tsc 时,这个错误就会消失。仅使用 tsc 也可以解决此错误stackoverflow.com/questions/36001159/…。你对“gulp-typescript”有什么经验吗?谢谢
    • 查找类型应该返回T | undefined
    【解决方案2】:

    经过调查,我注意到在 CMD 行中使用“tsc”来转换 Typsecript 时,一切都按预期工作。而不是在我的 gulp 文件中使用 'gulp.src' 我应该使用 'tsProject.src' 来模拟它。

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

    【讨论】:

    • 谢谢,正是我需要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2018-12-06
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多