I stumbled accross the following situation for the standard utility type Required:
type A = {x?: number;}
type B1 = Required<A>['x']; // number, OK
type B2<T extends A> = Required<T>['x']; // number, OK
class C1 {
public f(x: Required<A>['x']) { // x is number, OK
const y = x + 1; // OK
}
}
class C2<T extends A> {
public f(x: Required<T>['x']) { // x is number | undefined, NOT OK
const y = x + 1; // ERROR
}
}
I cannot imagine that this is expected behavior. Why is the C2-case different from the C1-case? I thought at first it may have something to do with generics in general, but only class generics seem to be affected since the B2-case works.
Can I do anything - other than explicitly saying something like x: Exclude<Required<T>['x'], undefined> - to make x (and potentially other properties of A) really required (i.e. not undefined)?