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

Kushagra Sharma picture Kushagra Sharma · May 28, 2016 · Viewed 9.9k times · Source

I have the following expression:

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

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)

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.
  */

As I see it, the argument is of type RegExp and so is the parameter in the definition. Then why the error?

Answer

Tamas Hegedus picture Tamas Hegedus · May 28, 2016

@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.