Property 'matchAll' does not exist on type 'string'
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"
]
}
}
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"]
}
Checking the ts signature for the String
and RegExp
classes I realized there is no signature for matchAll
. One manner to solve it could be:
let matches = str['matchAll'](regexp);
Another way would be adding the method to lib.es5.d.ts
file