Consider the following definitions:
interface A {
id?: string;
name?: string;
}
interface BaseLabel<B extends Parent, Parent extends A> {
keyStr?: keyof B & string;
}
interface RequiredLabel<B extends Parent, Parent extends A> extends BaseLabel<B, A> {
label: string;
}
interface OptionalLabel<B extends Parent, Parent extends A> extends BaseLabel<B, A> {
label?: string;
}
type Final<B extends Parent, Parent extends A> = RequiredLabel<B, Parent> | OptionalLabel<B, Parent>
I am seeking to enhance the Final
type so that Parent keys must have a required label (based on RequiredLabel
) while all other B keys can have an optional label (based on OptionalLabel
).
interface Parent extends A {
value?: string;
}
interface Child extends Parent {
isDate?: boolean;
}
const fields: Final[] = [
{id: '1', name: '1', isDate: true},
{id: '2', name: '2', isDate: true, label: 'label 2'},
{id: '3', name: '3', value: '3', label: 'label 3'},
];
All entries related to the parent entity must include a mandatory label, while others are optional.
Could someone assist me in achieving this specificity?