Search code examples
javascripttypescriptphpstorm

ES7 type hinting failure in PhpStorm


I am using TypeScript in my project. JavaScript's array includes() function has been valid since ECMA6. However, when I set my lib parameter in tsconfig to "es6" the following code throws a non-fatal error in the console browser.

['alpha', 'beta', 'gamma'].includes('alpha');

The non-fatal error:

[default] /foo/bar.component.ts:157:28 
Property 'includes' does not exist on type 'string[]'.

There is a simple solution. Changing the lib parameter in tsconfig to es7 silences the error in the console. All is well in the code.

However, PhpStorm 2016.3.2 does not recognize the solution. I continue to get a type hint error with the following message:

TS2339: Property 'includes' does not exist on type 'string[]'.

How can I get PhpStorm to recognize that the use of includes() is valid?


Solution

  • There are two fixes to the PHPStorm issue.

    1. Add a new library to PHPStorm's frameworks. The simplest solution is to go to PHPStorm > Preferences > Languages & Frameworks > JavaScript > Libraries and add the core-js-DefinitelyTyped library.
    2. If that does not work you can modify Nitzan Tomer's solution. If you keep the tsconfig lib value as es7 and add his code to polyfills.ts both the type hinting errors in PHPStorm will be resolved.