Skip to content

Inference doesn't find common base type for union with null or undefined - TypeScript 4.8 regressionΒ #49938

Closed
@dragomirtitian

Description

@dragomirtitian

Bug Report

πŸ”Ž Search Terms

inference common base type null undefined

πŸ•— Version & Regression Information

  • This changed between versions 4.7.3 and 4.8-beta

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function equal<T>(a: T, b: T) { }

let v = null!;

// Object types with common base types

type B = { foo: string }
type D = { foo: string; bar: number }

// Error in 4.8 TS can't find common type ❌
// 4.7 T was undefined | B
equal(v as B, v as undefined | D)

// ok T is B βœ…
equal(v as B, v as D)
// ok T is B | undefined βœ…
equal(v as B, v as B | undefined)

πŸ™ Actual behavior

In 4.8 TypeScript can't find common base type between B and undefined | D. In 4.7 this used to be B | undefined.

This also happens for literal types and their respective base types as well as for arrays as shown in the 'Other Examples' in the playground link. I didn't include the code as the bug is the same, it's just different instances we found in our code.

πŸ™‚ Expected behavior

Typescript finds the type B | undefined for T as it did in 4.7.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions