-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Description
Suggestion
π Search Terms
Map, map has
β Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
For instances cast as Map<>
, a check of Map#has(key)
should suffice as an undefined check for the key
π Motivating Example
Currently, we are required to have a redundant typecheck after a Map#has()
for Map#get()
calls:
const myMap : Map<string, string> = new Map();
myMap.set('key', 'value');
if (myMap.has('key')) {
// ts(2322) - 'string | undefined' is not assignable to type 'string'
const value : string = myMap.get('key');
}
This suggestion asks that ts infer the type as not a union with undefined if a Map#has()
check has proceeded the Map#get()
call
Metadata
Metadata
Assignees
Labels
No labels