坑2:tsc编译出现 XXX are only available when targeting ECMAScript 5 and higher
今天在练习typescript的时候,在用到存取器get和set的时候 出现了Accessors are only available when targeting ECMAScript 5 and higher.的报错,根据字面意思是说我们编译所用的ES版本太低,需要我们使用ES5及以上版本。命令行工具输入 tsc 可以看到当前的ES版本:
可以看到目前tsc编译器的目标ES版本是默认ES3的,如何更改目标版本?解决方案:
直接在命令后加上 --target ES5 参数即可,如:tsc test.ts --target ES5,或者简写为:tsc test.ts --t ES5