Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic infers literal type when there is a union and conditional type is involved

I stumbled upon this imho inconsistant behaviour:

A conditional alone works fine:

class FormControl<T> {
  constructor(t:T extends undefined ? never : T) {}
}
const a = new FormControl('');
//    ^? FormControl<string> // OK 

A union alone works also fine:

class FormControl2<T> {
  constructor(t:T|string) {}
}
const b = new FormControl2("");
//    ^? FormControl<string> OK 

When both union+conditional are combined :

class FormControl3<T> {
  constructor(t:T extends undefined ? never : T|string) {}
}
const c = new FormControl3(""); 
//    ^? FormControl<""> // Too narrow :( 

How could a get the wider string infered type in that 3rd case ? I'm looking for the widen inference not specifying each time time the correct type.

Playground

like image 765
Matthieu Riegler Avatar asked Oct 17 '25 00:10

Matthieu Riegler


1 Answers

It's actually a bug, or more of a limitation in TS 4.9.

This limitation has now been raised to 3 levels of nestings in TS 5.0.

See Issue #52620 on GitHub.

like image 58
Matthieu Riegler Avatar answered Oct 19 '25 14:10

Matthieu Riegler