facebook/flow

Parameters<> does not support optional params

ayroblu opened this issue · 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

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.

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.