From cdd119c7801ab3dd0f03adf78997c371c6703bb5 Mon Sep 17 00:00:00 2001 From: Icebob Date: Wed, 22 Nov 2023 20:41:34 +0100 Subject: [PATCH] improve types --- src/context.js | 3 ++- src/utils.js | 24 ++++++++++++------------ tsconfig.json | 2 +- types/extends.d.ts | 10 ++++++++++ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/context.js b/src/context.js index aa6fc0395..c3a286616 100644 --- a/src/context.js +++ b/src/context.js @@ -33,7 +33,8 @@ function mergeMeta(ctx, newMeta) { /** * Context class for action calls * - * @type import("./context").Context + * @typedef {import("./context")} ContextInterface + * @implements {ContextInterface} */ class Context { /** diff --git a/src/utils.js b/src/utils.js index e46318a65..421ea2757 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,4 @@ +// @ts-check /* * moleculer * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/moleculer) @@ -37,9 +38,9 @@ class TimeoutError extends ExtendableError {} /** * Circular replacing of unsafe properties in object * - * @param {Object=} options List of options to change circularReplacer behaviour - * @param {number=} options.maxSafeObjectSize Maximum size of objects for safe object converting - * @return {function(...[*]=)} + * @param {object} options List of options to change circularReplacer behaviour + * @param {number?} [options.maxSafeObjectSize = Infinity] Maximum size of objects for safe object converting + * @return {(key: string, value: any) => any} */ function circularReplacer(options = { maxSafeObjectSize: Infinity }) { const seen = new WeakSet(); @@ -172,7 +173,7 @@ const utils = { const interfaces = os.networkInterfaces(); for (let iface in interfaces) { for (let i in interfaces[iface]) { - const f = interfaces[iface][i]; + const f = interfaces[iface]?.[i]; if (f.family === "IPv4") { if (f.internal) { ilist.push(f.address); @@ -200,8 +201,7 @@ const utils = { /** * Polyfill a Promise library with missing Bluebird features. * - * - * @param {PromiseClass} P + * @param {typeof Promise} P */ polyfillPromise(P) { if (!utils.isFunction(P.method)) { @@ -381,8 +381,8 @@ const utils = { * Remove circular references & Functions from the JS object * * @param {Object|Array} obj - * @param {Object=} options List of options to change circularReplacer behaviour - * @param {number=} options.maxSafeObjectSize List of options to change circularReplacer behaviour + * @param {object} options List of options to change circularReplacer behaviour + * @param {number?} [options.maxSafeObjectSize = Infinity] Maximum size of objects for safe object converting * @returns {Object|Array} */ safetyObject(obj, options) { @@ -392,7 +392,7 @@ const utils = { /** * Sets a variable on an object based on its dot path. * - * @param {Object} obj + * @param {Record} obj * @param {String} path * @param {*} value * @returns {Object} @@ -400,7 +400,7 @@ const utils = { dotSet(obj, path, value) { const parts = path.split("."); const part = parts.shift(); - if (parts.length > 0) { + if (part && parts.length > 0) { if (!Object.prototype.hasOwnProperty.call(obj, part)) { obj[part] = {}; } else if (obj[part] == null) { @@ -436,7 +436,7 @@ const utils = { * Credits: https://github.com/visionmedia/bytes.js * * @param {String} v - * @returns {Number} + * @returns {Number|null} */ parseByteString(v) { if (typeof v === "number" && !isNaN(v)) { @@ -471,7 +471,7 @@ const utils = { * Get the name of constructor of an object. * * @param {Object} obj - * @returns {String} + * @returns {String|undefined} */ getConstructorName(obj) { if (obj == null) return undefined; diff --git a/tsconfig.json b/tsconfig.json index 8cd277927..e54510088 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "lib": ["ES2022"], }, "include": ["**.js"], - "exclude": ["node_modules", ".eslintrc.js", "prettier.config.js", "types"] + "exclude": ["node_modules", ".eslintrc.js", "prettier.config.js"] } diff --git a/types/extends.d.ts b/types/extends.d.ts index 5d58dd43e..b8d58c307 100644 --- a/types/extends.d.ts +++ b/types/extends.d.ts @@ -1,3 +1,13 @@ interface Promise { delay(ms: number): Promise; + method(fn: Function): Promise; + timeout(ms: number, message: String): Promise; + mapSeries(arr: Array, fn: Function): Promise; +} + +interface PromiseConstructor { + delay(ms: number): Promise; + method(fn: Function): Promise; + timeout(ms: number, message: String): Promise; + mapSeries(arr: Array, fn: Function): Promise; }