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

[TS 5.4.0-beta] Tuple type members being preserved #57389

Closed
acutmore opened this issue Feb 12, 2024 · 1 comment Β· Fixed by #57116
Closed

[TS 5.4.0-beta] Tuple type members being preserved #57389

acutmore opened this issue Feb 12, 2024 · 1 comment Β· Fixed by #57116
Assignees
Labels
Docs The issue relates to how you learn TypeScript

Comments

@acutmore
Copy link
Contributor

acutmore commented Feb 12, 2024

πŸ”Ž Search Terms

spread tuple type narrowing

πŸ•— Version & Regression Information

  • This changed between versions 5.3.3 and 5.4.0-beta
  • This changed in 5.4.0-dev.20240120

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-beta#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwHMQMAVAHhID4AKASgC54SBuAKFYAUYcBbLAZxAA6KBAjUA2kVJl+GGFlQEatADTwhm6eSioAnioC6tVvDNCMACxCpq1OP2QQMteAF5K8AN6mzfswD0AfAAegD8vv5geHLw1lDA7vAOThgSAAyGzP5+QfAArEIAzIy6euqFACyMcgpKkTnweeENZtGosRhQWBBJKc5C-BBYYCDUAIy02XmFRfBlFULV8GgA1qg4AO6oEoat-s0RjV098PxQ2PyIWCD8Z-KKBLvZB8FkALTv8Kggm-AgMG4MHgigKS0iAF8pqwgA

πŸ’» Code

declare function getT<T>(): T;

Promise.all([getT<string>(), ...getT<any>()])
   .then((result) => {
        // ^?
      const head = result[0];       // 5.3: any, 5.4: string
         // ^?
      const tail = result.slice(1); // 5.3 any, 5.4: unknown[]
         // ^?
      tail satisfies string[];     // <-- new error in 5.4
   });

Additional information about the issue

More of a 'change report' than a 'bug report', I don't think this was part of the 5.4 beta announcement.

@Andarist
Copy link
Contributor

This can be bisected to: #57031 and it's a bug.

When hovering over all we can see this:

(method) PromiseConstructor.all<[string, ...any[]]>(values: [string, ...any[]]): Promise<[string, ...unknown[]]> (+1 overload)

So how [string, ...any[]] was converted into [string, ...unknown[]] here? The reason is that now instantiateMappedTupleType has this ?? unknownType fallback when computing newElementTypes.

But it's not consistent with how mapped types continue to work today:

type Identity<T> = {
   [K in keyof T]: T[K]
}

type Result = Identity<[string, ...any[]]>
// ^? type Result: [string, ...any[]]

So the problem is that what we see in the original repro is actually not a true any but rather an errorType. And if we play around with it we can see the change in the behavior:

type Identity<T> = {
   [K in keyof T]: T[K]
}

type ErrorType = {}['prop']

type Result = Identity<[string, ...ErrorType[]]>
//   ^? 5.3: any (displayed as `any` but it's an `errorType`) 5.4: [string, ...unknown[]] 

The PR to which this has been bisected improves the situation (the output should be a tuple and not an any/errorType) but accidentally maps the thing to [string, ...unknown[]].

Why the errorType has crawled into all of this though? Well, I already have a fix for this πŸ˜‰ : #57116

@acutmore acutmore changed the title [TS 5.4.0-beta] Tuple type members being perserved [TS 5.4.0-beta] Tuple type members being preserved Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants