Dart - How to force map key to be contained in a list strings
If I understood it correctly (I don't know TypeScript)
Dart doesn't have key constraints so will have to extend the Map and implement those constraints. Here is code for that.
import 'dart:collection';class InvalidKeyError<K> extends Error { final Object key; final Set<K> keys; InvalidKeyError(this.key, this.keys); @override String toString() => "InvalidKeyError: $key not found in $keys";}class SpecialMap<K, V> extends MapMixin<K, V> { final Set<K> availableKeys; final Map<K, V> _map = {}; SpecialMap(this.availableKeys) : assert(availableKeys != null); @override Iterable<K> get keys => _map.keys; @override void clear() => _map.clear(); @override V remove(Object key) => _map.remove(key); @override V operator [](Object key) => availableKeys.contains(key) ? _map[key] : throw InvalidKeyError(key, availableKeys); @override operator []=(K key, V value) => availableKeys.contains(key) ? _map[key] = value : throw InvalidKeyError(key, availableKeys);}void main() { final availableKeys = {"cat", "dog"}; final map = SpecialMap<String, bool>(availableKeys); map["cat"] = true; map["dog"] = true; map["anything else"] = false; //will throw InvalidKeyError at runtime}
Although you can simply constrain through type if you want. Something like this.
enum Keys {cat, dog}void main(){ final Map<Keys, bool> map = {}; map[Keys.cat] = true; map[Keys.dog] = true; map["any other type"] = false; //complie time error}