From af0b7f34587bd432860a31d40eabc6aa70aef619 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 25 Nov 2020 23:54:45 -0800 Subject: [PATCH] fix: types for withTimeoutOptions (#3422) https://github.com/ipfs/js-ipfs/pull/3407/ has introduced regression in type inference for `withTimeoutOptions` function which end up referring to `Fn` type which was declared in other file https://github.com/ipfs/js-ipfs/pull/3407/files#diff-722621abc3ed4edc6ab202fdf684f1607c261394b95da6b3ec79748711056f20 I think TS has this strange WTF where when it can't figure out when JS file is a module it will treat it as if everything is loaded in the same scope. I think that caused `withTimeoutOptions` not to report errors but silently fail and infer return function as `any`. This change fixes that by removes `Fn` type all together, as it seemed like unnecessary complexity. Unfortunately nothing seemed to have caught this regression because we set [`noImplicitAny`](https://www.typescriptlang.org/tsconfig#noImplicitAny) to `false` given that many of our dependencies are untyped. --- packages/ipfs-core-utils/src/index.js | 6 ------ packages/ipfs-core-utils/src/with-timeout-option.js | 8 ++++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/ipfs-core-utils/src/index.js b/packages/ipfs-core-utils/src/index.js index 89aa433f85..ccacec309b 100644 --- a/packages/ipfs-core-utils/src/index.js +++ b/packages/ipfs-core-utils/src/index.js @@ -1,7 +1 @@ 'use strict' - -/** - * @template {any[]} ARGS - * @template R - * @typedef {(...args: ARGS) => R} Fn - */ diff --git a/packages/ipfs-core-utils/src/with-timeout-option.js b/packages/ipfs-core-utils/src/with-timeout-option.js index ace39cccce..7e20556bfd 100644 --- a/packages/ipfs-core-utils/src/with-timeout-option.js +++ b/packages/ipfs-core-utils/src/with-timeout-option.js @@ -7,15 +7,15 @@ const parseDuration = require('parse-duration').default const { TimeoutError } = require('./errors') /** - * @template {any[]} ARGS + * @template {any[]} Args * @template {Promise | AsyncIterable} R - The return type of `fn` - * @param {Fn} fn + * @param {(...args:Args) => R} fn * @param {number} [optionsArgIndex] - * @returns {Fn} + * @returns {(...args:Args) => R} */ function withTimeoutOption (fn, optionsArgIndex) { // eslint-disable-next-line - return /** @returns {R} */(/** @type {ARGS} */...args) => { + return /** @returns {R} */(/** @type {Args} */...args) => { const options = args[optionsArgIndex == null ? args.length - 1 : optionsArgIndex] if (!options || !options.timeout) return fn(...args)