Answer the question
In order to leave comments, you need to log in
How to correctly type a regular expression during destructuring?
How to correctly type a regular expression during destructuring?
const [a, b, c, d, f, g= ''] = value.match(/\d+/g);
TS swears at Type 'RegExpMatchArray | null' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
Answer the question
In order to leave comments, you need to log in
Are you sure it won't be null?
If yes, then you can just tell the compiler about it:
const [a, b, c, d, f, g= ''] = value.match(/\d+/g)!;
const [a, b, c, d, f, g= ''] = value.match(/\d+/g) || [];
Well, you are told in plain text: it match
can return null
and then an error will occur during execution.
Either like this:
const [a, b, c, d, f, g= ''] = value.match(/\d+/g) || [];
Or, if you are 146% sure that you match
will definitely find something:const [a, b, c, d, f, g= ''] = value.match(/\d+/g) as string[];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question