【发布时间】: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