报错:
error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.
原文:
1.gulpfile.js内容如下:
var gulp = require("gulp"),
tsc = require("gulp-typescript"),
typescript = require("typescript");
var tsProject = tsc.createProject({
removeComments: false,
noImplicitAny: false,
target: "es5",
module: "commonjs",
declarationFiles: false,
emitDecoratorMetadata: true,
typescript: typescript
});
gulp.task("build-source", function () {
return gulp.src(__dirname + "/file.ts")
.pipe(tsc(tsProject))
.js.pipe(gulp.dest(__dirname +"/"))
});
2.file.ts内容如下:
@logClass
class Person {
public name: string;
public surname: string;
constructor(name: string, surname: string) {
this.name = name;
this.surname = surname;
}
public saySomething(something: string): string {
return this.name + " " + this.surname + " says:" + something;
}
}
function logClass(target: any) {
var original = target;
function construct(constructor, args) {
var c: any = function() {
return constructor.apply(this, args);
}
c.prototype = constructor.prototype;
return new c();
}
var f:any = function(...args) {
console.log("NEW: " + original.name);
return construct(original, args);
}
f.prototype = original.prototype;
return f;
}
解决方法:
gulpfile.js文件添加experimentalDecorators并设置为true.
var tsProject = tsc.createProject({
... ...
experimentalDecorators: true,
});
网友评论