【问题标题】:Property 'replaceAll' does not exist on type 'string'“字符串”类型上不存在属性“replaceAll”
【发布时间】:2020-12-16 08:48:06
【问题描述】:

我想在 typescript 和 angular 10 中使用replaceAll

但我收到此错误:“字符串”类型上不存在属性“replaceAll”

这是我的代码:

let date="1399/06/08"
console.log(date.replaceAll('/', '_'))

输出:13990608

如何修复我的打字稿以显示此功能?

【问题讨论】:

  • TypeScript 无法神奇地添加浏览器未实现的功能。您需要实现Michael D's answer 中的链接问题中提到的替换方法之一。
  • date.split('/').join('_') 您现在可以使用它。虽然你可以更新到 chrome85。
  • 我想在vscode中使用我在vscode中有这个错误
  • @behroozbc 你可以试试最新的打字稿版本。

标签: angular typescript angular10


【解决方案1】:

您可以使用 RegExp 和 global flag 解决问题。全局标志是使 replace 在所有事件中运行的原因。

"1399/06/08".replace(/\//g, "_") // "1399_06_08"

【讨论】:

  • 这并不能“解决问题”。这是替换所有事件的另一种方法。但是如果你想继续使用replaceAll,这并不能解决OP的问题。
  • 这个问题是关于 TypeScript 与 String.replaceAll() 的兼容性,而不是请求解决方法。
【解决方案2】:

您应该能够通过您的tsconfig.json 添加这些类型。 在compilerOptions 中添加"ES2021.String"lib

您的 tsconfig 应如下所示:

{
    ...,
    "compilerOptions": {
        ...,
        "lib": [
          ...,
          "ES2021.String"
        ]
    }
}

replaceAll 方法在lib.es2021.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;
}

【讨论】:

  • 找到lib.esnext.string.d.ts 文件,这些函数在您的代码中声明不知道“通过您的 tsconfig.json 添加这些类型”是什么意思,您能否详细说明
  • @KukulaMula 我在答案中添加了一个模板。我的意思是,将ESNext.String 添加到lib 内部tsconfig.json
  • 这消除了 lint 错误,但是当我尝试运行时,我收到一条错误消息:An unhandled exception occurred: tsconfig.json:21:7 - error TS6046: Argument for '--lib' option must be: 'es5', 'es6', 'es2015',...,其中包含一长串选项,但它们都不是ESNext.String,并且无法编译。
  • @StackUnderflow 可能你的 TS 版本太低了,相信是 TS 2.4 中加入的,跟这个commit
  • @Josef 我的是 3.8.3,不是最新的,但也不是太旧。
【解决方案3】:

根据 MDN Web Docs,

"要执行全局搜索和替换,请将g 开关包含在 正则表达式”。

所以,你可以试试:

const date="1399/06/08"
const forwardSlashRegex = /(\/)/g;
console.log(date.replace(forwardSlashRegex, '_'));

这会自动将所有正斜杠替换为下划线。确保还在正则表达式末尾保留/g 全局指示符,因为它允许 JS 知道您要替换所有出现正斜杠的位置。

有关使用正则表达式指标的更多信息,请参阅以下非常有用的指南:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

【讨论】:

    【解决方案4】:

    你可以创建一个文件

    myOwnTypes.d.ts

    在你的 Angular 项目的根目录下并添加以下代码:

    interface String {
        replaceAll(input: string, output : string): any;
    }

    这将告诉 typescript 字符串具有此属性。

    现在 Chrome 和 Firefox 支持 replaceAll,但最好检查一下 caniuse 是否符合您的需求。

    https://caniuse.com/?search=replaceAll

    如果这对你有用,欢迎投票,我从这个 stackoverflow 帐户开始,希望得到支持:)

    【讨论】:

      【解决方案5】:

      Chrome 支持 replaceAll,因此可以安全使用。但是 typescript 仍然会出现错误,因此您可以将字符串转换为任何字符串,以克服该障碍。

      const date: any ="1399/06/08"
      console.log(date.replaceAll('/','_'))
      

      【讨论】:

      • 请不要使用any
      • 改成any不是解决办法!!!
      【解决方案6】:

      只需使用此功能

          let date="1399/06/08"
          
          console.log(date.split('/').join('_'))

      【讨论】:

      • 这是我解决问题的第一个想法,但我确信有更有效的方法。使用正则表达式似乎是要走的路
      【解决方案7】:

      来自docs

      截至 2020 年 8 月,Firefox 支持 replaceAll() 方法,但 不是通过 Chrome。它将在 Chrome 85 中可用。

      同时您可以找到多种其他方法here

      未来可能读者的截图:

      【讨论】:

      猜你喜欢
      • 2021-11-27
      • 2019-08-25
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多