microsoft / microsoft/TypeScript

3.4.1 regression in distributed types

Open
#31,121 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Conditional Types Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.5.0-dev.20190425

The example below was taken from a real world use-case and simplified.

The U type is distributed while the T type remains the original union.

The T type is wrapped with Box<T> when U is not an object.

type Box<T> = { value: T }
type Test<T> = [T] extends [infer U]
  ? U extends object
    ? U
    : U | Box<T>
  : never

type A = number | { foo: number }
type B = Test<A>

In theory, the B type should be identical to:

type B = number | { foo: number } | Box<number | { foo: number }>

But in the latest version, the B type is instead identical to:

type B = number | { foo: number } | Box<number | (number & { foo: number })>

This works correctly in 3.3.x but not 3.4.x and later.

Playground Link: click here

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Playground reproduction and compare its behavior between TypeScript 3.3.x and 3.4.x or later. Trace how the conditional type distributes U while preserving T, and consider the issue done when B matches the stated expected union without the intersection inside Box.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.