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
Inconsistency with Rest Parameter in Function TypeΒ #58552
This code does not display any type errors on any of the versions I tried, but will break at run time on the example1() call.
π Expected behavior
Typescript should prevent the assignment of (arg1: string) => void to a variable of type (...args: string[]) => void since it is possible to have args be empty.
Additional information about the issue
I attempted to search for other issues related to this and came up empty handed. This is different from the "I'm allowed to use a shorter parameter list where a longer one is expected" issue which has drowned out a lot of my searching.
a=(x: number)=>1;// ok, rest param corresponds to infinite number of params
This is a pretty old test though and it assumes strictNullChecks: false. I think it doesn't quite make sense in the strict world for this to be allowed. Rest params are effectively optional so why do we observe a difference here?
rest param corresponds to infinite number of params
Yeah, no. It can be up to an infinite number of params, but a minimum of zero. i.e. the same reason fn1 is an error in your example.
I guess the reasoning here is the same as how an object with a string index signature is treated in some contexts as having all possible properties at once, which is only rectified by enabling noUncheckedIndexedAccess.
This is an intentional affordance for functions like Regex#replace or (throwback) AMD's define call.
In practice any time you alias a function into a position with rest args, the number of arguments you'll get is almost always determinable at the call site (e.g. by the number of groups in the regex, the number of module names passed to define, and so on)
Activity
Andarist commentedon May 17, 2024
I found this in the existing tests:
TypeScript/tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignaturesWithRestParameters.ts
Line 16 in 79a8514
This is a pretty old test though and it assumes
strictNullChecks: false. I think it doesn't quite make sense in the strict world for this to be allowed. Rest params are effectively optional so why do we observe a difference here?We can find some recent tests with
strict: truethat would break if the above wouldn't work:TypeScript/tests/cases/compiler/assignmentToAnyArrayRestParameters.ts
Lines 6 to 14 in 79a8514
At the very least
f2andf4should error here - assuming that the issue at hand would get fixed.fatcerberus commentedon May 17, 2024
Yeah, no. It can be up to an infinite number of params, but a minimum of zero. i.e. the same reason
fn1is an error in your example.I guess the reasoning here is the same as how an object with a
stringindex signature is treated in some contexts as having all possible properties at once, which is only rectified by enablingnoUncheckedIndexedAccess.RyanCavanaugh commentedon Jun 14, 2024
This is an intentional affordance for functions like Regex#replace or (throwback) AMD's
definecall.In practice any time you alias a function into a position with rest args, the number of arguments you'll get is almost always determinable at the call site (e.g. by the number of groups in the regex, the number of module names passed to
define, and so on)RyanCavanaugh commentedon Jun 14, 2024
I could swear we discussed this on GitHub (not CodePlex) but I can't find it - very hard to search for
typescript-bot commentedon Jun 17, 2024
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
mkantor commentedon Jul 4, 2025
Maybe you were thinking of the discussion in #9352?