javascriptflowtypeecmascript-2021

Flow does not support certain JavaScript (ECMAScript) built-in methods, what do I do?


As an embedded software engineer used to strictly-typed languages, I was compelled to use Flow (https://flow.org) when writing an experimental utility in JavaScript.

About 10 minutes into writing code with JS+Flow, Flow ('flow-bin' binary) gave me the following error:

Cannot call string.trim().replaceAll because property replaceAll (did you mean replace?) is missing in String [1].

with regards to the following (Flow-annotated) code:

static sanitizeString(string : string) : string {
    return string.trim().replaceAll(/(?:\s+|\.+)/g, "");
}

Flow tells me that it can not find the (fairly-widely implemented) string.prototype.replaceAll(...) function in (what seems to be a) "symbolic" definitions file: tmp/flow/flowlib_1fa18dde633e97c7_501/core.js

Which begs the question:

Are Flow users solely at the mercy of the provisioned definitions file when it comes to using (widely-implemented) methods defined by ECMAScript standards (or proposals/drafts for that matter, given the fast-paced nature of browsers' implementation of ECMAScript)?

Do I have to modify the definitions file (core.js) to resolve this problems?


Solution

  • First of all, I don't have direct answer to this question, but I'll try to elaborate the problem a bit.

    But this is rather interesting. It truly throws that error: Flow Example

    Now, it gets even more interesting since TypeScript throws the same error: TS Example

    However, TS is more robust with the error message:

    Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the lib compiler option to 'esnext' or later.

    Hence, I suggest you to follow that advice and switch flow compiler to follow different set of rules (in .flowconfig I guess?).