Skip to content

Parameters<> does not support optional params #9125

Open
@ayroblu

Description

@ayroblu
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

Activity

SamChou19815

SamChou19815 commented on Mar 5, 2024

@SamChou19815
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.

added
Typing: completenessNo false positives (type checker claims that there are some errors in the correct program)
and removed on Mar 5, 2024
self-assigned this
on Mar 5, 2024
SamChou19815

SamChou19815 commented on Mar 8, 2024

@SamChou19815
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

Metadata

Metadata

Assignees

Labels

Typing: completenessNo false positives (type checker claims that there are some errors in the correct program)bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Parameters<> does not support optional params · Issue #9125 · facebook/flow