flow icon indicating copy to clipboard operation
flow copied to clipboard

Parameters<> does not support optional params

Open ayroblu opened this issue 11 months ago • 2 comments

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

ayroblu avatar Mar 05 '24 14:03 ayroblu

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 avatar Mar 05 '24 18:03 SamChou19815

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.

SamChou19815 avatar Mar 08 '24 01:03 SamChou19815