【问题标题】:Gulp + browserify + typescript - type expectedGulp + browserify + typescript - 预期类型
【发布时间】:2017-04-12 20:22:50
【问题描述】:

我无法运行此脚本。每次它都会给我一个错误:

-> % gulp compile-js [22:18:57] 使用 gulpfile ~/wwwdata/projects/site/gulpfile.js [22:18:57] 开始 '编译-js'...

events.js:141 投掷者; // 未处理的“错误”事件 ^ TS1110:app/Resources/src/tsc/Material.ts(22,19):TS1110:预期类型。

第 22 行如下所示:

private _xhr: null | JQueryXHR;

如果我把它改成

private _xhr:any;

然后我得到:

TS1005: app/Resources/src/tsc/Material.ts(147,33): TS1005: '=' 预期。

这是为这个方法中的for循环生成的:

/** load threads/comments for current loaded material */
getThreads() {

    /** if there is no more threads to load just exit the function */
    if(this.isMoreThreads == false) {
        return;
    }

    let url = Routing.generate('comments', {
        type: this.materialType,
        id: this.materialId,
        page: this.page
    });


    if (this._xhr) {
        this._xhr.abort();
    }

    this._xhr = $.ajax(url, {
        method: 'POST',
        beforeSend: () => {
        },
        success: (response) => {

            if(!(response.hasOwnProperty('threads'))) {
                this.isMoreThreads = false;
                $(document.getElementById('loadMoreThreads')).remove();
                return;
            }

            //for (let thread in response.threads) { // <- if change 'of' to 'in' then there is no error
            for (let thread of response.threads) {
                let thr = new MaterialThread(thread);
                this.addThread = thr;

                this._commentsContainer.append(thr.render());
            }
        },
        error: () => {
            let act = confirm('Error has occurred. Refresh page?');
            if (act === true) {
                window.location.reload();
            }
        },
        complete: () => {
            this._commentsContainer.find('#loading').fadeOut();
            this._xhr = null;
        }
    });
}

我的gulpfile.js:

var tsconfig = {
    target: "es5",
    module: "commonjs",
    declaration: false,
    noImplicitAny: false,
    removeComments: true,
    noLib: false
};

gulp.task('compile-js', function() {
    var bundler = browserify({
        basedir: paths.typescript,
        debug: true,
    })
        .add(paths.typescript + '/index.ts')
        .plugin(tsify, tsconfig)
        .transform(debowerify);

    return bundler.bundle()
        .pipe(exorcist('/dupa/application.js.map'))
        .pipe(source('application.js'))
        .pipe(gulp.dest('/dupa'));
});

【问题讨论】:

  • 据我所知,null 只允许使用 void 和 undefined 使用管道运算符。它们是兄弟类型,只能在声明和其他类似您尝试做的事情之间分配。
  • @Fals,好的,我们把它改成any。第二个错误呢?
  • TS1005 表示缺少某些东西,如果您可以提供更多代码,我们可以帮助您找到问题。一个常见的错误是当我们没有指定类型时,用 : 赋值。您在此之前/之后的某些属性有错误。
  • 另外,如果你仔细观察,错误发生的那行变成了(147,33),这就是新的问题。
  • @Fals 我更新了我的问题

标签: typescript gulp browserify


【解决方案1】:

您必须将 exclude 添加到您的 tsconfig 文件中,以消除此错误 TS1110: app/Resources/src/tsc/typings/jquery.d.ts(1491,54): TS1110

var tsconfig = {
    target: "es5",
    module: "commonjs",
    declaration: false,
    noImplicitAny: false,
    removeComments: true,
    noLib: false,
    "exclude": [
        "node_modules",
        "typings"
    ]
};

【讨论】:

  • arghhh,现在我遇到了这个问题:> TS1153:'let' 声明仅在针对 ECMAScript 6 及更高版本时可用。
  • @breq 你应该将你的节点更新到最新版本,还有你的打字稿版本,我在这里尝试了一些代码,其中 let 以 ES5 为目标并且运行良好。
  • 我正在使用 typescript v 2.2.2 和 node v 4.2.6。这是我的实际 gulpfile.js pastebin.com/1vwGc5Pz 和我的 tsconfig.json pastebin.com/jHVx9e4G
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 2015-10-02
  • 2021-12-21
  • 1970-01-01
  • 2017-03-02
  • 2016-07-13
  • 2015-04-12
相关资源
最近更新 更多