4

Quite a simple question, but I haven't found an answer yet anywhere: Is there some switch to make TypeScript compile arrow functions into plain JavaScript functions?

I use them a lot in my code, and I don't want to rewrite everything. But I lately realized, that IE doesn't support them.

I already tried to switch the script version to ES5, but then my code won't compile anymore, because I'm using "filter" as well, which doesn't seem to be a part of it. However, I don't know, if that would do the job in the first place.

2 Answers 2

5

If you want to just convert your arrow functions to regular functions and keep the rest of the code compiling, you can set the target config to es5 and lib property to es6:

"compilerOptions": {
    "target": "es5",
    "lib": ["es6"],
    // ....
}

This will allow your code that uses ES6 features to compile while targeting ES5. But you have to make sure those features (like filter) are available at runtime. If they are not available it will throw runtime exceptions.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your suggestion. I changed my setting accordingly. However, now it throws 221 errors, including such weird things as "Build: Name "document" could not be found." (Translated from German). It also can't find stuff such as XMLHttpRequest, navigator, and even HTMLElement. What's going wrong there?
Add "lib": ["es6", "dom"] if you are building for the browser.
Thank you. I already found that answer in the mean time by myself. Everything works fine now, even in IE. I just had to add another polyfill. You really saved my day!
1

Arrow functions are a part of ES6, but you should be able to transpile your code to ES5 with Babel. There is a babel-preset-typescript which you should be able to use for this.

1 Comment

Thank you for your idea. I already solved it with @Saravana's solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.