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
Inconsistent behavior of rest tuple with optional elements followed by non-optional elements #51643
typeT1=[number?];typeT2=[...T1,string];// [number | undefined, string], but should be [number | undefined, string] | [string]constt1: T1=[];constt2: T2=[...t1,"hello"];// ["hello"]declarefunctiongetUser(id: string,options?: {x?: string}): string;declarefunctiongetOrgUser(id: string,orgId?: number,options?: {x?: string}): void;functioncallApi<Textendsunknown[]=[],U=void>(method: (...args: [...T,{x?: string}])=>U){return(...args: [...T])=>method(...args,{});}callApi(getUser);// (id: string) => stringcallApi(getOrgUser);// (id: string, orgId?: string | undefined) => void, but should be (id: string, orgId: string | undefined) => void
🙁 Actual behavior
As pointed out when middle and leading rest elements in tuple types were introduced, optional elements are spread as if the element are non-optional but possibly undefined (1)(2). When a tuple with omitted optional values is spread as a non-trailing rest into a tuple, the values are shifted and don't match the tuple's type.
Related inference behavior was addressed in #39607, but currently a variadic rest tuple with optional elements can be inferred when the rest tuple is followed by non-optional elements. This is incorrect, because the inferred type permits omitted values, resulting in invalid spreads where elements are shifted around.
🙂 Expected behavior
Spreading a tuple with optional elements should result in a type that accounts for potentially omitted optional elements.
Similarly, inferred variadic rest tuples should be restricted to only non-optional elements when followed by non-optional elements, because permitting omitted values results in invalid spreads where elements are shifted around.
Activity