[英]es6 features not compiling in TypeScript
I'm new to TypeScript and I'm having some trouble compiling es6 code. 我是TypeScript的新手,我在编译es6代码时遇到了一些麻烦。
I have a .ts file: 我有一个.ts文件:
let a: number[] = [1,34,5,5,34,3];
console.log(a.find(no => no == 5));
When I run tsc --module es6 --target es2015 src/test.ts
it compiles fine, however tsc --module es6 --target es5 src/test.ts
doesn't seem to work? 当我运行
tsc --module es6 --target es2015 src/test.ts
它编译得很好,但是tsc --module es6 --target es5 src/test.ts
似乎不起作用? I would like to target es5, but doing so gives me the error: 我想针对es5,但这样做会给我错误:
Property 'find' does not exist on type 'number[]'.
Can't TypeScript compile to es5, or will I need to run babel on top of the TypeScript compiler? TypeScript不能编译成es5,还是我需要在TypeScript编译器上运行babel?
Typescript can compile to es5, the only problem is that the find
method is defined in the es2015
standard, so typescript will assume it will not exist. Typescript可以编译成es5,唯一的问题是
find
方法是在es2015
标准中定义的,所以es2015
会认为它不存在。
Typescript has two ways of controlling what standard you compile to target
which tells the compiler how to compile the syntax and lib
which tells the compiler what the runtime environment looks like (see here for more detailed answer with examples) Typescript有两种方法来控制你编译到
target
标准,它告诉编译器如何编译语法和lib
,它告诉编译器运行时环境是什么样的(有关示例的详细解答,请参见此处 )
To target es5
but get the find
method you need to also specify the lib: 要定位
es5
但获取find
方法,您还需要指定lib:
tsc --module es6 --target es5 src/test.ts --lib es2015,dom,scripthost
Note that typescript will provide no polyfills, you either bring your own or hope find
exists in the runtime. 请注意,typescript将不提供polyfill,您可以自带或希望
find
存在于运行时中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.