I am attempting to find a nullable type in a conditional type:
interface Unwrapped { dummyProp: string; }
interface UnwrappedArray<T extends Unwrapped> extends Array<T> { }
interface Wrapped<T extends Unwrapped> { unwrapped: T; }
type ToPropertyWrapped<T, K extends keyof T> =
T[K] extends UnwrappedArray<infer ArrayElementType> ? Wrapped<ArrayElementType>[] :
T[K] extends Unwrapped ? Wrapped<T[K]> :
NonNullable<T[K]> extends Unwrapped ? Wrapped<NonNullable<T[K]>> | undefined :
T[K];
However, I keep encountering this error...
error TS2344: Type 'NonNullable<T[K]>' does not meet the requirement 'Unwrapped'.
Type 'T[K]' cannot be assigned to type 'Unwrapped'.
...when trying to use
Wrapped<NonNullable<T[K]>>
. What steps can I take to resolve this compile error?
// example implementation that generates the compile error mentioned above
interface PropType extends Unwrapped {
}
interface Test extends Unwrapped {
prop: PropType;
nullableProp: PropType | undefined;
arrayProp: PropType[];
numberProp: number;
}
let t: ToPropertyWrapped<Test, "prop">; // Wrapped<PropType>
let u: ToPropertyWrapped<Test, "nullableProp">; // Wrapped<PropType> | undefined
let v: ToPropertyWrapped<Test, "arrayProp">; // Wrapped<PropType>[]
let w: ToPropertyWrapped<Test, "numberProp">; // number