When working on an ecmascript project, I make use of @ts-check
and a ts linter to follow TypeScript's formalism without actually using TypeScript itself.
Everything is running smoothly except for one issue: I am trying to implement JSDoc generics declaration to have the return type dependent on or inferred from a parameter type:
/**
* @template V - Type de valeurs de la map.
* @param {Map<string, V>} map
* @returns {Object.<string, V>}
*/
function objectifyMap(map) {
/** @type {Object.<string,V>} */
const result = {};
map.forEach((value, key) => { result[key] = value; });
return result;
}
/**
* @template V
* @param {Object.<string, V>} object
* @returns {Map<string, V>}
*/
function mappifyObject(object) {
/** @type {Map<string,V>} */
const result = new Map();
Object.entries(object).forEach(([key, value]) => { result.set(key, value); });
return result;
}
/** @type {Object.<string, string>} */
const obj = {};
/** @type {Map<string, string>} */
let map;
map = mappifyObject(obj);
However, I encounter an error with the obj
parameter on the last line according to the ts linter:
Argument of type '{ [x: string]: string; }' is not assignable to parameter of type '{ [x: string]: V; }'.
Index signatures are incompatible.
Type 'string' is not assignable to type 'V'.
'V' could be instantiated with an arbitrary type which could be unrelated to 'string'.ts(2345)
I suspect that the ts linter expects something like
map = mappifyObject<string>(obj);
but since I'm only utilizing @ts-check
and not the full TypeScript compiler, I cannot do that directly.
Is there any workaround for this situation?