4

我有以下表达式:

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

4

2 回答 2

4

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

于 2016-05-28T14:53:58.493 回答
0

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

于 2021-07-18T06:34:05.997 回答