gpt4 book ai didi

angular - 属性 'replaceAll' 在类型 'string' 上不存在

转载 作者:行者123 更新时间:2023-12-04 04:27:34 24 4
gpt4 key购买 nike

我想用replaceAll在 typescript 和 Angular 10。
但我收到此错误:类型“字符串”上不存在属性“replaceAll” .
这是我的代码:

let date="1399/06/08"
console.log(date.replaceAll('/', '_'))
输出:13990608
如何修复我的 typescript 以显示此功能?

最佳答案

您应该能够通过您的 tsconfig.json 添加这些类型。 .
添加 "ESNext.String"libcompilerOptions .
您的 tsconfig 应该如下所示:

{
...,
"compilerOptions": {
...,
"lib": [
...,
"ESNext.String"
]
}
}
replaceAll方法在 lib.esnext.string.d.ts 中定义如下:
interface String {
/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replaceAll(searchValue: string | RegExp, replaceValue: string): string;

/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replacer A function that returns the replacement text.
*/
replaceAll(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
}

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

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