diff --git a/map.js b/map.js index 65cbc17..ad2c642 100644 --- a/map.js +++ b/map.js @@ -18,9 +18,9 @@ export const create = () => new Map() * Copy a Map object into a fresh Map object. * * @function - * @template X,Y - * @param {Map} m - * @return {Map} + * @template K,V + * @param {Map} m + * @return {Map} */ export const copy = m => { const r = create() @@ -37,12 +37,12 @@ export const copy = m => { * ``` * * @function - * @template V,K - * @template {Map} MAP + * @template {Map} MAP + * @template {MAP extends Map ? function():V : unknown} CF * @param {MAP} map - * @param {K} key - * @param {function():V} createT - * @return {V} + * @param {MAP extends Map ? K : unknown} key + * @param {CF} createT + * @return {ReturnType} */ export const setIfUndefined = (map, key, createT) => { let set = map.get(key) diff --git a/map.test.js b/map.test.js index 55f5432..2f47bf4 100644 --- a/map.test.js +++ b/map.test.js @@ -26,3 +26,30 @@ export const testMap = _tc => { t.assert(!map.all(m, (v, k) => k === 1 && v === 2)) t.assert(map.all(m, (v) => v === 2 || v === 3 || v === numberOfWrites)) } + +/** + * @param {t.TestCase} _tc + */ +export const testTypeDefinitions = _tc => { + // setIfUndefined supports inheritance properly: See https://github.com/dmonad/lib0/issues/82 + class A { + constructor () { + this.a = 4 + } + } + class B extends A { + constructor () { + super() + this.b = 4 + } + } + /** + * @type {Map} + */ + const m = map.create() + /** + * @type {B} + */ + const b = map.setIfUndefined(m, 0, () => new B()) + console.log(b) +}