Closed
Description
Search Terms:
generics widen type
Code
type Widen<T extends number | string> = T extends number
? number
: T extends string
? string
: T;
class Foo<T extends number | string> {
public constructor(private readonly validValues: readonly T[]) { }
// UNEXPECTED ERROR: T is not assignable to Widen<T>
public isValidValue(value: Widen<T>): value is T {
return this.validValues.indexOf(value as any) !== -1;
}
}
Expected behavior:
The Widen
type in my example widens number and string types. For any type T
, T
is assignable to Widen<T>
. I expect to be able to rely on this fact in generics.
Actual behavior:
The assignability relationship of T
and Widen<T>
is lost when T
is a generic type rather than a concrete type (see // UNEXPECTED ERROR
comment in example).