This question is about .d.ts
as declarations for .js
files.
I'm trying to declare a function that has a generic inside a generic but I can't seem to get it right. The function is essentially an Array#forEach
function for objects ({[key: string]: E}
).
I tried the following, but VSCode intellisense doesn't seem to comprehend the types of the property values of the object.
Typings File:
export module Util {
export function forEach<K, T extends { [key: string]: K }>(obj: T, callbackfn: (value: K, key: string, object: T) => void): void;
}
JavaScript Call:
if (undefined) var { Util } = require("./globals");
/** @type {{ [key:string]: number }} */
var obj = {};
Util.forEach(obj, function (value, key, object) { });
Source code of forEach
:
function forEach(obj, callbackfn) {
if (!(obj instanceof Object)) throw Error("Util.forEach called on non object");
for (var key in obj) callbackfn.call(obj, obj[key], key, obj);
}