【问题标题】:Typescript: Argument of type 'RegExpMatchArray' is not assignable to parameter of type 'string'打字稿:“RegExpMatchArray”类型的参数不可分配给“字符串”类型的参数
【发布时间】:2016-09-26 18:37:25
【问题描述】:

我有以下表达:

import { persistState } from 'redux-devtools';

const enhancer = compose(
  applyMiddleware(thunk, router, logger),
  DevTools.instrument(),
  persistState(
    window.location.href.match(/[?&]debug_session=([^&]+)\b/)
  )
);

我得到一个参数以匹配函数并出现以下错误

“RegExpMatchArray”类型的参数不能分配给 输入“字符串”。用正则表达式匹配字符串,并返回 包含该搜索结果的数组。 (方法) String.match(regexp: RegExp): RegExpMatchArray (+1 重载)

VSCode 中的 peek 定义显示:

match(regexp: string): RegExpMatchArray;
/**
  * Matches a string with a regular expression, and returns an array containing the results of that search.
  * @param regexp A regular expression object that contains the regular expression pattern and applicable flags.
  */
match(regexp: RegExp): RegExpMatchArray;

/**
  * Replaces text in a string, using a regular expression or search string.
  * @param searchValue A string that represents the regular expression.
  * @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
  */

在我看来,参数是 RegExp 类型,定义中的参数也是。那为什么会报错呢?

【问题讨论】:

  • 你对比赛的结果做了什么?
  • 请提供MCVE

标签: typescript visual-studio-code typescript1.8 typescript-typings


【解决方案1】:

@NitzanTomer 是对的。类型不匹配的不是 match 函数参数。您当然会尝试将匹配函数的返回值存储在字符串类型变量中/从:string 函数中返回/作为string 参数传递。

【讨论】:

  • 我认为这是 VSCode 智能感知的问题。 swiggly 仅在要匹配的参数上,而整个语句 window.location.href.match(/[?&]debug_session=([^&]+)\b/) 应该有 swiggly。重新启动 VSCode 修复它。
  • 感谢@KushagraSharma,重新启动 vscode 确实也是我的解决方案。您是否知道是否已报告此错误?
  • @MaximeBernard 我不知道这个问题是否被报告了。
【解决方案2】:

重新启动 VS Code Typescript 服务器为我修复了它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 2019-10-30
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多