错误 TS1056: 在 gulp-typescript 中只有当目标 ECMAScript 5 时才可用访问器。

11

在使用gulp-typescript将TS转译为JS时,我遇到了一个错误信息。我尝试使用一个ES5特性来获取/设置属性。

错误 TS1056:访问器仅在针对 ECMAScript 5 及更高版本时可用

我该如何使转译器目标定位到es5?

我在互联网上搜索了解决方案,建议您设置target = es5并将其传递给typescript。我已经使用一个tsconfig.json文件进行了以下操作:

tsconfig.js

{
  "compilerOptions": {
    "target": "es5"
  },
  "files": []
}

gulp 任务

import gulp from 'gulp';
import gulpif from 'gulp-if';
import livereload from 'gulp-livereload';
import typescript from 'gulp-typescript';
import args from './lib/args';

const tsProject = typescript.createProject('tsconfig.json');

console.log(tsProject);

gulp.task('scripts-typescript', () => {
    return gulp.src('app/scripts/**/*.ts')
        .pipe(typescript(tsProject()))
        .pipe(gulp.dest(`dist/${args.vendor}/scripts`))
        .pipe(gulpif(args.watch, livereload()));
});

已记录的输出

输入图像描述


你有什么问题? - JB Nizet
4个回答

5

我所做的是使用 "tsc --target ES5 YourFile.ts" 编译了这个ts文件。


1
请不要在多个问题上重新发布相同的答案。如果这些问题是相同的,请标记它们为重复。 (几乎不需要声望,容易获得。)如果这些问题不同,请根据问题调整您的答案。 - Baum mit Augen
1
这并不是很相关。如果您要运行TypeScript编译器,建议使用的方式与使用tsconfig相同。但是,OP正在使用gulp,您的答案与gulp或tsconfig无关。 - loctrice

3

gulp-typescript插件有一个名为"target"的选项。我发现设置tsconfig.json文件没有任何效果,但当我在gulp任务中将目标改为es5时,它可以正常工作。

插件选项

...
    .pipe(typescript(tsProject(), { target: 'ES5'}))
...

0

试试这个

  • 打开终端/cmd并导航到包含你的ts文件的目录。
  • 使用以下命令。 tsc filename.ts --target ES5

-1
在 Windows 操作系统和 Visual Studio Code 中的控制台窗口中键入以下命令: tsc -target "es5" yourFilename.ts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接