I am looking to provide a JsDoc with type signature for the following JavaScript function:
function addExtraData(f, extra) {
return (props, ...args) => f({ ...props, extra }, ...args)
}
My attempt at defining the JsDoc is not quite right:
/**
* @param {(props: Props, ...args: Args) => Result} f
* @param {Extra} extra
* @template Extra
* @template {{ extra: Extra }} Props
* @template {Array} Args
* @template Result
* @returns {(props: Omit<Props, 'extra'>, ...args: Args) => Result}
*/
I have defined Omit
as
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
If this cannot be achieved in a JsDoc, I am open to creating a .d.ts
file instead.
update
A refined version that works externally:
/**
* @template {Object} P
* @template {Array} A
* @template R
* @param {(props: P, ...args: A) => R} f
* @param {P['extra']} extra
* @returns {(props: Omit<P, 'extra'>, ...args: A) => R}
*/
export function addExtraData(f, extra) {
return (props, ...args) => f(Object.assign({}, props, { extra }), ...args)
}
However, there is an error being produced for Object.assign
:
Argument of type
is not assignable to parameter of type 'P'. [2345]Pick<P, Exclude<keyof P, "extra">> & { extra: P["extra"]; }
In my understanding, this should result in P
, excluding extra
and intersecting it with something containing extra
of the correct type.