Flow: regex String .match flow type wrong

Created on 22 Mar 2017  Â·  14Comments  Â·  Source: facebook/flow

Currently it's defined as ?Array<string> for return type.
However, the returned object is something like
["12:12a", "a", index: 0, input: "12:12a"]
https://github.com/facebook/flow/blob/v0.41.0/lib/core.js#L286

Most helpful comment

I suppose it must be something like this:

declare class RegExpMatchArray extends Array<string> {
    index?: number;
    input?: string;
}

and

match(regexp: string | RegExp): RegExpMatchArray | null;

All 14 comments

Hi. Is there any way to override this in local project?

String.prototype.match will return null if there is no match. For example, "abc".match(/foo/) evaluates to null.

@samwgoldman - this is the array extending thing we discussed last week.

Someone tried to improve it in https://github.com/facebook/flow/commit/9611080b787711a392b8d5548241ecb4ed784e6b but I had to revert in https://github.com/facebook/flow/commit/3107fb3394cce82abd3c964e992d1178da33fb2d 😢

I suppose it must be something like this:

declare class RegExpMatchArray extends Array<string> {
    index?: number;
    input?: string;
}

and

match(regexp: string | RegExp): RegExpMatchArray | null;

oh, I misunderstood the original post. sorry about that.

so maybe re-open this issue?
thanks @samwgoldman!

I'd use:
```
declare class RegExpMatchArray extends Array {
index: number;
input: string;
}

and for `String.match`:

match(needle: string | RegExp): Array | RegExpMatchArray | null;

and for `RegExp.prototype`:

exec(haystack: string): RegExpMatchArray | null;
```

@qm3ster your fundamental issue is that Flow doesn't understand properly the filter step and so doesn't rule out null results. See here

I believe this is a different issue than this one.

I'm surprised, as from my testing I thought Flow let's you do whatever you want when converting from string arrays to string tuples. See this for example.

This is correct if the input is a RegExp:

> "STRING".match('S')
[ 'S', index: 0, input: 'STRING' ]
> "STRING".match(/.{1,3}/g)
[ 'STR', 'ING' ]

Butt currently flow says the returned value is undefined :-( Why would this be undefined?

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swBnABxkPwElsAVAJ0KoFN8ywAKQ7CzPgBc5fC2wBzAJQiAgkyYBDAJ4AeMmL4SAfGADeqMGFwkNYTe05gAvGD4D8AOgC2i-LgAW3YI70BGABoAZgBfYGl0I0IoHgsYDi5rJLBsTBgYKX1DI3MPJkQUtiQAUQU4Jm4AA1xFbGw4fHIqGjAAEj17QRC7bHw4cxZLMkqpbJDIsCYOTCZSOITUEKA

?T means T | null | undefined, you only handled the null case in your example (ie. use == instead of === to fix it).

Also see:

  • Duplicate issue #2450 – Native String#match declaration incorrect
  • PR #3007 – Fixes String#match and RegExp#exec declarations
  • PR #3359 – Add new lib def for String#match() result

Seems like the landscape has changed a bit in the last year. Opened a PR to try something like this: https://github.com/facebook/flow/pull/6790

I believe that this can now be closed due to #6790. @asolove @mrkev Could you help to close this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

doberkofler picture doberkofler  Â·  3Comments

mjj2000 picture mjj2000  Â·  3Comments

ghost picture ghost  Â·  3Comments

marcelbeumer picture marcelbeumer  Â·  3Comments

jamiebuilds picture jamiebuilds  Â·  3Comments