Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: types for withTimeoutOptions (#3422)
Browse files Browse the repository at this point in the history
#3407 has introduced regression in type inference for `withTimeoutOptions` function which end up referring to `Fn<Args, R>` 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.
  • Loading branch information
Gozala authored Nov 26, 2020
1 parent 79c2c80 commit af0b7f3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
6 changes: 0 additions & 6 deletions packages/ipfs-core-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
'use strict'

/**
* @template {any[]} ARGS
* @template R
* @typedef {(...args: ARGS) => R} Fn
*/
8 changes: 4 additions & 4 deletions packages/ipfs-core-utils/src/with-timeout-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const parseDuration = require('parse-duration').default
const { TimeoutError } = require('./errors')

/**
* @template {any[]} ARGS
* @template {any[]} Args
* @template {Promise<any> | AsyncIterable<any>} R - The return type of `fn`
* @param {Fn<ARGS, R>} fn
* @param {(...args:Args) => R} fn
* @param {number} [optionsArgIndex]
* @returns {Fn<ARGS, R>}
* @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)

Expand Down

0 comments on commit af0b7f3

Please sign in to comment.