Skip to content

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

Closed
@acutmore

Description

@acutmore

πŸ”Ž 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.

Activity

added
DocsThe issue relates to how you learn TypeScript
on Feb 12, 2024
Andarist

Andarist commented on Feb 13, 2024

@Andarist
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

changed the title [-][TS 5.4.0-beta] Tuple type members being perserved[/-] [+][TS 5.4.0-beta] Tuple type members being preserved[/+] on Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

DocsThe issue relates to how you learn TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @DanielRosenwasser@acutmore@RyanCavanaugh@Andarist

    Issue actions

      [TS 5.4.0-beta] Tuple type members being preserved Β· Issue #57389 Β· microsoft/TypeScript