type NonNullableCopy<O> = {
[p in keyof O] -?: O[p] extends null | undefined ? never : O[p];
};
type Adsa = {a?: number | null}
type Basda = NonNullableCopy<Adsa>
let asd : Basda = {
a: null // Still valid. No errors
}
Although it seems similar, the following code:
type NonNullable<T> = T extends null | undefined ? never : T;
type NonNullableCopy<O> = {
[p in keyof O] -?: NonNullable<O[p]>;
};
type Adsa = {a?: number | null}
type Basda = NonNullableCopy<Adsa>
let asd : Basda = {
a: null // Works, but throws errors
}
There is actually a key difference between these two blocks of code. Can you spot it? Please explain to me. Thank you.