question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flow analysis doesn't work with es6 collections 'has' method

See original GitHub issue

TypeScript Version: 2.1.1

Code

const x = new Map<string, string>();
x.set("key", "value");
if (x.has("key")) {
  const y : string = x.get("key");  // error: y is string | undefined, not string
}


Expected behavior: y is narrowed down to string Actual behavior: y is still string | undefined even after checking if the map has that key

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:123
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

23reactions
ivstascommented, Jan 16, 2017

@DanielRosenwasser I think you’re a bit overcomplicating the stuff.

const map = new Map<string, string>();
const value = <string>(map.has('key') ? map.get('key') : 'default value');

Anyway, these all are workarounds. What’s need to be fixed if flow analysis.

20reactions
DanielRosenwassercommented, Dec 29, 2016

Actually, you can get this to work better with another overload.

const x = new Map<string, string>();

interface Map<K, V> {
    // Works if there are other known strings.
    has<KnownKeys extends string, CheckedString extends string>(this: MapWith<string, V, KnownKeys>, key: CheckedString): this is MapWith<K, V, CheckedString | KnownKeys>

    has<CheckedString extends string>(this: Map<string, V>, key: CheckedString): this is MapWith<K, V, CheckedString>
}

interface MapWith<K, V, DefiniteKey extends K> extends Map<K, V> {
    get(k: DefiniteKey): V;
    get(k: K): V | undefined;
}

x.set("key", "value");
if (x.has("key") && x.has("otherKey")) {
  const a: string = x.get("key"); // works!
  const b: string = x.get("otherKey"); // also works!
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - FlowType Does Not Recognize ES6 Function Import
This error is generated when executing "yarn run flow". This same code compiles and executes without issue. – Nathan G. Jan 11, 2018...
Read more >
You Don't Know JS: ES6 & Beyond - GitHub Pages
An iterator is a structured pattern for pulling information from a source in one-at-a-time fashion. This pattern has been around programming for a...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
Here, { x++; } is the block statement. This outputs 2 because the var x statement within the block is in the same...
Read more >
ECMAScript® 2023 Language Specification - TC39
◢5.2 Algorithm Conventions ... 6.1.7.1 Property Attributes; 6.1.7.2 Object Internal Methods and Internal Slots ... 8.1 RS: Evaluation; ◢8.2 Scope Analysis.
Read more >
You Don't Know JS: ES6 &amp; Beyond
Async Flow Control. ... “You Don't Know JavaScript: ES6 & Beyond by Kyle Simpson ... function declaration was the immediately invoked function expres‐....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found