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

Inconsistent behavior of rest tuple with optional elements followed by non-optional elements #51643

Open
JamiesWhiteShirt opened this issue Nov 25, 2022 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@JamiesWhiteShirt
Copy link

Bug Report

🔎 Search Terms

variadic tuple optional element rest spread inference

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about spreading tuples with optional elements

⏯ Playground Link

Playground link with relevant code

💻 Code

type T1 = [number?];
type T2 = [...T1, string]; // [number | undefined, string], but should be [number | undefined, string] | [string]
const t1: T1 = [];
const t2: T2 = [...t1, "hello"]; // ["hello"]

declare function getUser(id: string, options?: { x?: string }): string;

declare function getOrgUser(id: string, orgId?: number, options?: { x?: string }): void;

function callApi<T extends unknown[] = [], U = void>(method: (...args: [...T, { x?: string }]) => U) {
    return (...args: [...T]) => method(...args, {});
}

callApi(getUser);  // (id: string) => string
callApi(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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants