打字稿:"RegExpMatchArray"类型的参数无法分配给“字符串"类型的参数 [英] 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/)
)
);
我在参数匹配上出现了混乱,并出现以下错误
I get a swiggly on the argument to match function with the following error
"RegExpMatchArray"类型的参数无法分配给的参数 输入字符串".将字符串与正则表达式匹配,然后返回 包含该搜索结果的数组. (方法) String.match(regexp:RegExp):RegExpMatchArray(+1重载)
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中的偷看定义显示:
The peek definition in VSCode shows:
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,定义中的参数也是如此.那为什么会出错呢?
As I see it, the argument is of type RegExp and so is the parameter in the definition. Then why the error?
推荐答案
@NitzanTomer是正确的.类型不匹配的不是匹配功能参数.您当然可以尝试将match函数的返回值存储在字符串型变量中/从:string
函数返回它/将其作为string
参数传递.
@NitzanTomer is right. It's not the match functions parameter that has a type mismatch. You certainly try to store the return value of the match function in a string typed variable / return it from a :string
function / pass it as a string
parameter.
这篇关于打字稿:"RegExpMatchArray"类型的参数无法分配给“字符串"类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!