Example situation:
type F = {
(thing: string): void;
(things: string[]): void;
}
type Z = Parameters<F>[0]
Type Z here resolves to string[]
, not string | string[]
, because overloads just get the last declared one matched.
I am curious to know if this is possible to implement. As far as I can tell this also affects the standard function overload approach, e.g.:
function fun(thing: string): void;
function fun(stuff: string[]): void;
// function fun(thing: string | string[]); // uncomment me to allow type Y to match `string`.
function fun(thing: string | string[]) { console.log(Array.isArray(thing) ? thing.join('; ') : thing); }
type F = {
(thing: string): void;
(things: string[]): void;
}
type Z = Parameters<F>[0]
type Y = Parameters<typeof fun>[0];
let zz:Z = 'abc';
let yy:Y = 'abc';
To be clear, what I'm asking for is a util e.g. OverloadParameters
which in this case would produce for type OverloadParameters<F>
a value of [string] | [string[]]
. I'm actually currently unsure of how to unwrap the parameter-array situation there but I'm sure it's manageable. As expected, the type ([string] | [string[]])[0]
is string | string[]
.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too