2

I know that Typescript use ES6 or ES2015.

And I know that anArray.includes('ifExist'); only available in ES6.

But why can't I use it when I use Typescript? It says that anArray don't have a method includes.

My Array

anArray = [
    {
        'category': 'x',
        'data': []
    }, 
    {
        'category': 'y',
        'data': []
    }, 
    {
        'category': 'z',
        'data': []
    }
];

ifExist variable

ifExist = {
    'category': 'a',
    'data': []
};

If I use anArray.indexOf(isExist) < 0, I can get the result like anArray.includes(isExist).

3
  • 1
    Your anArray is probably not typed as an array. You need to provide code that reproduces the problem Commented Sep 9, 2018 at 11:35
  • I've edited my question for more explanation :d Commented Sep 9, 2018 at 11:41
  • 2
    According to this it's in ES2016. Commented Sep 9, 2018 at 11:42

1 Answer 1

8

You need to specify target in your ts-config.json to ES2016 (or higher) in order to use Array.includes.

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

4 Comments

I see, it's my mistake too, includes method is in ES2016, not ES2015. And I use ES2015. I thought includes is in ES2015. Then this answer will definitely solve my problem. Thank you. :)
Is there a place where this information is kept? I was thinking maybe MDN would tell me which version of EcmaScript this was introduced but it doesn't seem to 😕
@AhmedFasih I mainly use node.green to check this, although its purpose is to list which ecmascript features are available in which node version.
Oh interesting! Yes I see it under "Node.js ES2016 Support"! Thank you!! I was grepping for another function in this directory: github.com/microsoft/TypeScript/tree/main/src/lib 😅 this is much better

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.