gpt4 book ai didi

typescript - 类型 'matchAll' 上不存在属性 'string'

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:29 49 4
gpt4 key购买 nike

我想在字符串上应用正则表达式。为了获得所有组的结果,我使用了 matchAll 方法。这是我的代码

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

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

编译上述代码时出现错误

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

在搜索这个错误的过程中,我在 stackoverflow 上发现了类似的问题

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

我更改了上面链接中提到的 tsconfig 配置,但我的问题没有解决

这是我的 tsconfig 代码;

{
"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()是 ECMAScript 2020 规范(草案)的一部分。在 TypeScript 中,您可以通过在编译器选项中添加 es2020es2020.string 来包含这些库功能:

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

关于typescript - 类型 'matchAll' 上不存在属性 'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55499555/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com