Skip to content

5.1 regression: constraints of generic tuple types are sometimes order-sensitiveΒ #54495

@alecgibson

Description

@alecgibson

Bug Report

πŸ”Ž Search Terms

  • 5.1
  • tuple
  • array
  • spread

Possibly related:

πŸ•— Version & Regression Information

  • This changed between versions v5.0.4 and v5.1.3

⏯ Playground Link

I was unable to reproduce: if you could please steer me in the right direction I'm happy to dig more, but I'm a bit stuck on how to debug this

πŸ’» Code

We have an issue using io-ts. If this is a bug with that library, I'll close this here, but I don't think it is.

The TypeScript compiler is complaining about a number of excerpts from the d.ts file, where the compiler doesn't seem to be making sense.

Here's an example error:

node_modules/io-ts/lib/index.d.ts:1151:24 - error TS2344: Type 'CS' does not satisfy the constraint '[Mixed, Mixed, Mixed[]]'.
  Type '[Mixed, Mixed, ...Mixed[]]' is not assignable to type '[Mixed, Mixed, Mixed[]]'.
    Target allows only 3 element(s) but source may have more.

1151 ) => TaggedUnionC<Tag, CS>

The code it's referring to is here:

export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]>
  extends TaggedUnionType<Tag, CS, TypeOf<CS[number]>, OutputOf<CS[number]>, unknown> {}

export declare const taggedUnion: <Tag extends string, CS extends [Mixed, Mixed, ...Mixed[]]>(
  tag: Tag,
  codecs: CS,
  name?: string
) => TaggedUnionC<Tag, CS>

As you can see, the compiler seems to be collapsing the [Mixed, Mixed, ...Array<Mixed>] to [Mixed, Mixed, Mixed[]], which is incorrect. It then doesn't like CS extending [Mixed, Mixed, ...Mixed[]], which it considers incompatible with its incorrect collapsing.

Weirdly, if I tweak the d.ts file by hand, the error goes away:

- export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]>
+ export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Mixed[]]>

πŸ™ Actual behavior

Compilation errors

πŸ™‚ Expected behavior

Successful compilation

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds More InfoThe issue still hasn't been fully clarified

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions