Skip to content

call of methods get undefined #2751

@steambap

Description

@steambap

I have a map of type:

Map<string, string | string[]>

I call the get() methods after I run a has() check on the Map Object, and flow warns me about incompatible type. Why?

Here is part of my code:

if (sectionBody.has(key)) {
	const prevVal: string | string[] = sectionBody.get(key);
	sectionBody.set(key, [].concat(prevVal, value));
}

And this is the error message

74:                    const prevVal: string | string[] = sectionBody.get(key);
                                                           ^^^^^^^^^^^^^^^^^^^^ call of method `get`
 74:                    const prevVal: string | string[] = sectionBody.get(key);
                                                           ^^^^^^^^^^^^^^^^^^^^ undefined. This type is incompatible with
 74:                    const prevVal: string | string[] = sectionBody.get(key);
                                       ^^^^^^^^^^^^^^^^^ union: string | array type
  Member 1:
   74:                  const prevVal: string | string[] = sectionBody.get(key);
                                       ^^^^^^ string
  Error:
   74:                  const prevVal: string | string[] = sectionBody.get(key);
                                                           ^^^^^^^^^^^^^^^^^^^^ undefined. This type is incompatible with
   74:                  const prevVal: string | string[] = sectionBody.get(key);
                                       ^^^^^^ string
  Member 2:
   74:                  const prevVal: string | string[] = sectionBody.get(key);
                                                ^^^^^^^^ array type
  Error:
   74:                  const prevVal: string | string[] = sectionBody.get(key);
                                                           ^^^^^^^^^^^^^^^^^^^^ undefined. This type is incompatible with
   74:                  const prevVal: string | string[] = sectionBody.get(key);
                                                ^^^^^^^^ array type

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions