0

In the following example, how could I use a typeguard on the dictionaryHasEntry function in order to infer that dictionnary.get(key) is not undefined ?

const dictionary = new Map<string, any[]>()

function dictionaryHasEntry(key:string) { // Should infer that dictionnary.get(key) is of type any[]
  return !!dictionary.get(key)
}


function addValueToExistingEntry(key:string, value: any) {
  if(dictionaryHasEntry(key)){
    const existingEntry = dictionary.get(key) // existingEntry should be infered as of type any[]
    existingEntry.push(value) // Don't want to use "existingEntry!" here.
  }
}

Code example here

1
  • If key is just of type string then there's nothing you can do since the compiler won't be able to remember that you're using the same key in the dictionaryHasEntry() call and in the get() call. You don't want the compiler to think that all string keys have values in the map. You'd need to narrow down key from string to some string literal type for that to begin to work. Why not just call get() and check it for undefined?
    – jcalz
    Aug 14, 2020 at 15:33

2 Answers 2

0

The least crazy way to approach this is to just call get() and then use the return value of undefined to indicate that the entry does not exist. If your map value type could possibly be undefined, that would be a problem, but undefined is not assignable to any[], so it's a reasonable way to distinguish presence from absence:

const dictionary = new Map<string, any[]>()

function addValueToExistingEntry(key: string, value: any) {
  const existingEntry = dictionary.get(key);
  if (existingEntry !== undefined) {
    existingEntry.push(value)
  }
}

If you want to start using type guard functions to narrow the map to a type with a definite value at the given key, you need to use lots of generics and specifically have the key be of a type narrower than string, like a generic constrained to string or like a string literal type. Like this:

function dictionaryHasKey<
  M extends Map<string, any>,
  K extends string
>(dict: M, key: K): dict is { get(k: K): M extends Map<any, infer V> ? V : never } & M {
  return dict.has(key);
}

And then your original code will work:

function hmmAddValueToExistingEntry<K extends string>(key: K, value: any) {
  if (dictionaryHasKey(dictionary, key)) {
    const existingEntry = dictionary.get(key);
    existingEntry.push(value);
  }
}

But that's a lot of work and advanced type-system hoop jumping compared to just checking existingEntry for being undefined.


Okay, hope that helps; good luck!

Playground link to code

0

So first of all I would change the name of the method, because it implies the value is existing, although the undefined check needs to be performed inside of the method. In addition, Typescript maps support the has method, which returns you a boolean if a map contains a key. I would do it like that.

function addValueToExistingEntry(key:string, value: any) {
    if (dictionary.has(key)) {
      const existingEntry = dictionary.get(key);
      if (existingEntry) { 
        existingEntry.push(value);
        dictionary.set((key), existingEntry);
      }
    } else {
      dictionary.set(key, value)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.