Property 'matchAll' does not exist on type 'string'

Yousuf picture Yousuf · Apr 3, 2019 · Viewed 16k times · Source

I want to apply Reg Expression on string. In order to get all groups result i am using matchAll method. Here is my code

const regexp = RegExp('foo*','g'); 
const str = "table football, foosball";
let matches = str.matchAll(regexp);

for (const match of matches) {
   console.log(match);
}

during compiling on above code i got error

Property 'matchAll' does not exist on type '"table football, foosball"'

during searching about this error i found similar issue on stackoverflow

TS2339: Property 'includes' does not exist on type 'string'

I changed tsconfig configuration as mention in above link but my issue did not solved

Here is my tsconfig code;

{
 "compileOnSave": false,
 "compilerOptions": {
 "baseUrl": "./",
 "importHelpers": true,
 "outDir": "./dist/out-tsc",
 "sourceMap": true,
 "declaration": false,
 "module": "es2015",
 "moduleResolution": "node",
 "emitDecoratorMetadata": true,
 "experimentalDecorators": true,
 "target": "es2016",
 "typeRoots": [
  "node_modules/@types"
 ],
"lib": [
  "es2018",
  "dom"
]
}
}

Answer

mvr picture mvr · Jul 31, 2019

String.prototype.matchAll() is part of the ECMAScript 2020 specification (draft). In TypeScript you can include these library features by adding es2020 or es2020.string, in the compiler options:

"compilerOptions": {
    "lib": ["es2020.string"]
}