You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lossy argument names when spreading across parameter typesΒ #48049
type Equality = (a: number, b: number) => boolean;
type GOOD = (...args: [...Parameters<Equality>]) => void;
type BAD = (...args: [...Parameters<Equality>, string]) => void;
π Actual behavior
As shown here, the argument names are lost in if the argument list expression involves any arguments without names:
π Expected behavior
It should infer the type of BAD as (a: number, b: number, args_2: string) => void.
π€ Workaround
Declaring an extra function type with the additional arguments seems to work:
type Equality = (a: number, b: number) => boolean;
type Additional = (lol: string) => void; // π
type GOOD = (...args: [...Parameters<Equality>]) => void;
type BAD = (...args: [...Parameters<Equality>, ...Parameters<Additional>]) => void;
Inlining a function type seems to work too:
type Equality = (a: number, b: number) => boolean;
type GOOD = (...args: [...Parameters<Equality>]) => void;
type BAD = (...args: [...Parameters<Equality>, ...Parameters<(lol: string) => void>]) => void;
So there are known workarounds, although I would say these are non-obvious - it took me a long time to figure this out, and it's just surprising how the second part of the parameter type expression seems to determine the outcome of the first part.
(it's a factory function in a test-framework - it takes a simple (...args: any) => boolean assertion function, and converts it into a function that accepts additional details and produces a Fact - while it produces a working result, the parameter names are crucial to the understanding of it, and this is essentially useless without the argument names.)
(on that note, being able to infer documentation would be extremely useful in this case as well.)
I do think there is room for some improvements here.
Maybe the error messages could be more helpful?
It's definitely not clear to me why the obvious solution in my original post only works until you wrap it in a type for reuse. Something seems inconsistent here? If there is some practical reason why this doesn't / can't / shouldn't work, perhaps the 5048 error message ("Tuple members must all have names or all not have names") should appear when this is attempted?
The short answer is that the parameter -> tuple name feature is "best effort" (since nothing in the type system works/doesn't work depending on these names), and the effort to handle this particular case simply hasn't been done yet
Activity
mindplay-dk commentedon Feb 27, 2022
Actually, this workaround didn't work for my case either.
Here's the same workaround wrapped in a
WithMoreParamstype expression:As soon as you're doing it from a function declaration or another type expression, it loses the names again.
Playground link
mindplay-dk commentedon Feb 27, 2022
If you're interested in the real-world use-case, here it is:
(it's a factory function in a test-framework - it takes a simple
(...args: any) => booleanassertion function, and converts it into a function that accepts additionaldetailsand produces aFact- while it produces a working result, the parameter names are crucial to the understanding of it, and this is essentially useless without the argument names.)(on that note, being able to infer documentation would be extremely useful in this case as well.)
IllusionMH commentedon Feb 27, 2022
Tuple either all have labeled elements or none. In your example none are.
See what #39941 (comment) for details.
Much simpler and clear way
mindplay-dk commentedon Feb 28, 2022
I had no idea you could name tuple members! (is that a documented feature? I've never seen this before.)
Using this approach does seem to work:
Curiously though, the compiler demands that I name
paramshere - even though the name is unused and discarded.Apparently, tuple members can only have names if the parent tuple has a name?
But yes, this approach works for my use case as well:
Thanks Andrii Dieiev (@IllusionMH) !
I do think there is room for some improvements here.
Maybe the error messages could be more helpful?
It's definitely not clear to me why the obvious solution in my original post only works until you wrap it in a type for reuse. Something seems inconsistent here? If there is some practical reason why this doesn't / can't / shouldn't work, perhaps the 5048 error message ("Tuple members must all have names or all not have names") should appear when this is attempted?
jcalz commentedon Feb 28, 2022
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#labeled-tuple-elements
RyanCavanaugh commentedon Feb 28, 2022
The short answer is that the parameter -> tuple name feature is "best effort" (since nothing in the type system works/doesn't work depending on these names), and the effort to handle this particular case simply hasn't been done yet
fix(typings): properly type emits with timeout (2)
Vanilagy commentedon Oct 19, 2023
The original reproduction
is fixed now:
I say this issue can be closed, then.