Last active
April 5, 2020 21:25
-
-
Save slikts/3a064ee7f00b688a7a8b97e40b6dec61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class DefaultMap<K, V> extends Map<K, V> { | |
constructor(private init: (key: K) => V) { | |
super(); | |
} | |
get(key: K): V { | |
if (!this.has(key)) { | |
const value = this.init(key); | |
this.set(key, value); | |
return value; | |
} | |
return super.get(key)!; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment