Let's take a closer look at the error messages you received:
When trying to call TokenService.getToken(...).then with a function bound to onFulfill, Flow indicates that property read is missing in String [1] within the first argument.
This error message means that Flow believes that result
is a String, and therefore cannot access the property result.read
as Strings do not have this property ("property read is missing
"). Instead of focusing on logging the variable type of read
, it would be beneficial to log its contents and verify if it is indeed a valid String.
The second error message states:
Attempting to call download with read bound to readToken reveals that the object type [1] is incompatible with string [2].
This implies that Flow perceives the type of read
as something other than a String in the line:
download(url_template, name, read, is_watermarked);
It is important to ascertain whether read
is genuinely a string before attempting to set its type or override it (in Typescript, this would typically be done through (read as string)
, although I am uncertain about how Flow handles this situation).
Remember that what you observe in the console at runtime differs from Flow's assumptions about a variable's type, as Flow can only make educated guesses regarding a variable's type without certainty about its behavior during runtime.
===========================
I want to clarify that the following answer is inaccurate; it has been retained here to avoid confusing individuals who may come across comments referencing this response
The issue arises when examining the result
object in this context:
const { read } = result;
If you notice that the type of result
is not explicitly defined, you can enforce a strong type for the read
variable under the assumption that your API consistently delivers correct responses with strings located under result.read
. This can be accomplished by casting the read
variable as a string:
download(url_template, name, (read as string), is_watermarked);
Please bear in mind that inspecting a variable through logging differs from working within a Typescript environment. Your approach essentially verified the runtime value, whereas Typescript primarily operates at the build level and might not anticipate the precise nature of the API responses.