✨TypedMap
subclass of Map that checks key/value types.
JS ⟩ value ⟩ object ⟩ class ⟩ example ⟩ TypedMap
key points:
must call superclass constructor
super()
before using this.use
super.method()
to call superclass methods in subclass methods.
replit ⟩ TypedMap
// superclass
class TypedMap extends Map {
// private properties
#keyType;
#valueType;
// ⭐ subclass constructor
constructor(keyType, valueType, entries) {
// ⭐ must init new object with `super()` first❗
super(); // ⭐ superclass constructor
// ⭐ now `this` is accessable.
this.#keyType = keyType;
this.#valueType = valueType;
// type-check/add entries if `entries` provided
if (entries) {
// subclass uses its method to help set key/value pairs,
// make sure all properties are fully intialized before doing this❗
entries.forEach(([key, value]) => this.set(key, value));
}
}
// ⭐ override superclass method❗
set(key, value) {
// type check first, throw error if failed.
this.#typecheck(key, value);
// then set [key, value] using superclass method
super.set(key, value); // ⭐ superclass method
}
// check types
#typecheck(key, value) {
// ⭐ destructuring private fields not allowed❓
// ---------------------------------------------------------
// const {#keyType: keyType, #valueType: valueType} = this;
// ^^^^^^^^ (⛔ SyntaxError: Unexpected identifier)
const keyType = this.#keyType;
const valueType = this.#valueType;
if (typeof key !== keyType || typeof value !== valueType)
throw new TypeError(`[${key}:${value}] is not of type [${keyType}:${valueType}]`);
}
}
// export
module.exports = TypedMap;
const TypedMap = require('./TypedMap.js');
// test run
const dict = new TypedMap('string', 'string');
dict.set('hi', 'hello');
dict.get('hi') // 'hello'
// dict.set(1, 'one');
// TypeError: [1:one] is not of type [string:string]
const a = new TypedMap('number', 'string', [
[1, 'one'], [2, 'two'],
]);
a.get(1) // 'one'
Last updated