Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters<> does not support optional params #9125

Open
ayroblu opened this issue Mar 5, 2024 · 2 comments
Open

Parameters<> does not support optional params #9125

ayroblu opened this issue Mar 5, 2024 · 2 comments
Assignees
Labels
bug Typing: completeness No false positives (type checker claims that there are some errors in the correct program)

Comments

@ayroblu
Copy link

ayroblu commented Mar 5, 2024

declare type F = (s: string, n?: number) => boolean;
type Tuple = Parameters<F>;
declare function g(...args: Parameters<F>): boolean;
g("hi")

Cannot call g because rest array [1] has 1 element but tuple type [2] has 2 elements. [invalid-tuple-arity]

Flow version: master

Expected behavior

Type is tuple with optional arity

Actual behavior

Tuple is fixed length

@SamChou19815
Copy link
Contributor

Reduced example so we can see this more clearly: link

It seems a result of Flow forgetting that the element is optional after inference, and reduces it to T | void instead.

@SamChou19815 SamChou19815 added Typing: completeness No false positives (type checker claims that there are some errors in the correct program) and removed needs triage labels Mar 5, 2024
@SamChou19815 SamChou19815 self-assigned this Mar 5, 2024
@SamChou19815
Copy link
Contributor

Hi. I did some investigation on this and make some attempts to fix it, but unfortunately the root of the issue is harder to address than I anticipated.

An obvious fix is to track the optionality properly. However, function types don't enforce similar optionality check like tuple types. Due to utility types like this that can easily convert between function params and tuple types, the connection is deep enough that we have to fix both at once, otherwise you will get some confusing errors elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Typing: completeness No false positives (type checker claims that there are some errors in the correct program)
Projects
None yet
Development

No branches or pull requests

2 participants