Open TheEdward162 opened 3 years ago
The change between v4.1.5 and v4.2.3 occurred at 22bee779d729547ca1ea90b1575f34a6f178ad21.
:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.
Issue body code block by @TheEdward162
:x: Failed: -
Type 'AndThenResult' is not assignable to type 'AndThenResult'.
Type '{ kind: "success"; value: number | undefined; }' is not assignable to type 'AndThenResult'.
Type '{ kind: "success"; value: number | undefined; }' is not assignable to type '{ kind: "success"; value: number; }'.
Types of property 'value' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
Version | Reproduction Outputs |
---|---|
5.0.2, 5.1.3, 5.2.2, 5.3.2, 5.4.2 |
:x: Failed: -
|
Bug Report
🔎 Search Terms
undefined, generic inference, typescript 4.2
🕗 Version & Regression Information
4.2.0-beta - 4.3.0-dev.20210504 (everything since 4.2 beta, including all stable releases) NOT in 4.1.x or before
The return type inference of the the arrow function hasn't changed, according to the playground it was always infered as
{ kind: "failure"; value?: undefined; } | { kind: "success"; value: number; }
. However, before 4.2. the type of the generic function above it (theandThen
) was correctly narrowed down toAndThenResult<number>
. This happened even without explicit type annotations, as one would expect.In 4.2+ the undefined gets propagated into
andThen<number | undefined>
and this breaks the assignment (and my expectations).⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The types don't match and the code doesn't compile. The return type of
andThen(fn)
is different than that expected ona
.🙂 Expected behavior
The types do match, the function return type is narrowed to
AndThenResult<number>
and nothing breaks.