【发布时间】:2015-05-04 15:24:34
【问题描述】:
我一直在尝试为我的前端开发工作设置一个 gulpfile,但我遇到了一些问题。大多数都很容易修复,但我不知道这里发生了什么。
我正在使用的相关内容是:
- 打字稿
- 车把
- 吞咽
- Browserify(使用 tsify)
设置(希望只有相关部分)如下:
gulpfile.js:
gulp.task('handlebars', function() {
return gulp.src('./src/templates/**/*.hbs')
.pipe(handlebars())
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
noRedeclare: true,
namespace: "templates",
processName: function(filePath) {
return declare.processNameByPath(filePath.replace('templates/', ''));
}
}))
.pipe(concat('hbs.js'))
.pipe(wrap('var Handlebars = require("handlebars");\n<%= contents %>'))
.pipe(gulp.dest('./src/templates'))
});
main.ts:
/// <reference path="../typings/hbs.d.ts" />
import hbs = require("hbs.js");
var template = hbs.templates["src"]["index"];
hbs.d.ts:
declare module "hbs.js" {
export var templates: {
[index: string]: (model: any) => string;
}
}
我从 gulp-handlebars readme 和 hbs.d.ts 借用了 gulp 任务从 StackOverflow answer。到现在为止还挺好。 gulp 输出的templates.js 文件长这样:
var Handlebars = require("handlebars");
this["templates"] = this["templates"] || {};
this["templates"]["src"] = this["templates"]["src"] || {};
this["templates"]["src"]["index"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
return "Hello world\n";
},"useData":true});
我做错了,因为我收到了一个错误:"TS7017: src/ts/main.ts(11,24): TS7017: 对象类型的索引签名隐含一个'any ' 类型。” 我查看了错误,但 it seems to be 我必须设置一个显式索引类型,但我的代码中的 [index: string] 不正是这样做的吗?还是整个声明都错了?
我已取消设置 noImplicitAny,因此我可以继续工作,但我喜欢这个选项并希望继续使用它。
【问题讨论】:
-
不完全确定你想在这里做什么,但要么你使用的“模板”界面错误,要么你定义了错误的模板。请在下面查看我的答案。
标签: typescript gulp handlebars.js