打字稿:'RegExpMatchArray' 类型的参数不可分配给 'string' 类型的参数

Typescript: Argument of type 'RegExpMatchArray' is not assignable to parameter of type 'string'

我有以下表达式:

import { persistState } from 'redux-devtools';

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

我在 match 函数的参数上出现了错误,出现以下错误

Argument of type 'RegExpMatchArray' is not assignable to parameter of type 'string'. Matches a string with a regular expression, and returns an array containing the results of that search. (method) String.match(regexp: RegExp): RegExpMatchArray (+1 overload)

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 类型,定义中的参数也是。那为什么报错呢?

@NitzanTomer 是对的。类型不匹配的不是匹配函数参数。您肯定会尝试将匹配函数的 return 值存储在字符串类型变量中/return 来自 :string 函数/将其作为 string 参数传递。

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