I'm new to TypeScript and I'm having some trouble compiling es6 code.
I have a .ts file:
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? I would like to target es5, but doing so gives me the error:
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 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 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)
To target es5
but get the find
method you need to also specify the 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.