Hi all, I'm trying to make a RequiredField generic that takes a Type T and a true/false param. If True, only Type T should be allowed. If false, then Type T | undefined should be allowed.
However, with the following example, if I omit testField, Typescript errors saying that testField is required.
type RequireField<
T,
IsRequired extends boolean = false
> = IsRequired extends true ? T : T | undefined;
export interface Test<IsPopulated extends boolean = false> {
testField: RequireField<Uint8Array, IsPopulated>;
}
// Typescript complains that testField is required here.
const test: Test<false> = {};
Can anyone see what I'm doing wrong here? I've used a similar pattern to allow other types (e.g. T | string) and that seems to work okay.