In summary, I’m trying to define a function like function.prototype.bind()
but one which uses a function as follows:
/**
* @template T
* @template {any[]} A
* @template {any[]} B
* @template R
* @param {(this: T, ...params: [...A, ...B]) => R} func
* @param {() => [T, ...A]} getParams
*/
export function bindReturn(func, getParams) {
/** @param {B} params */
return (...params) => func.call(...getParams(), ...params)
}
However, when I use it as follows, TS is not able to correctly infer the type of parameter B
/**
* @param {string} a
* @param {number} b
* @param {boolean} c
*/
function f(a, b, c) {
return /** @type {const} */ ([a, b, c])
}
const f2 = bindReturn(f, () => /** @type {const} */ ([null]))
Here, the type parameter B
of bindReturn
is inferred as any[]
, instead of [a: string, b: number, c: boolean]
. I used [...A, ...B]
because it is also used in the definition of .bind()
in lib.d.ts
, so I was expecting the above to have no issues. Is something wrong with what I’ve written, or is it just an issue with the compiler?