Typescript Map key auto complete

Viewed 133

how do I make a Map which it's keys are auto complete when you try get the key.

Like:

const map: Map<string, string> = new Map();
map.set("key1", "value");
map.get(/* auto complete key here */)
2 Answers

Yoy can use type keyword to create your own custom types, use a bar to make it a union type allowing it to accept multiple different values like the following example

type Key = 'key1' | 'key2';

let map = new Map<Key, string>();

map.get();

enter image description here

Code example here.

You can create an enum as key in the map to make it auto-complete.

enum Key {
    Number = 1,
    Float = 2
}

let map = new Map<Key, string>();

map.get(Key.Float);

Playground link: Click here

Related