I'm exploring the possibility of enhancing an interface by adding extra options to it. For example:
interface IRole {
id: number;
name: string;
}
interface IAddress {
line1: string;
line2: string;
city: string;
state: string;
zip: string;
}
interface IUser {
email: string;
password: string;
role?: IRole;
addresses: IAddress[];
}
const options: FieldOptions<IUser> = {
name: 'role',
description: 'This is a role',
children: [
{
name: 'name', // The child element declaration should be the type that is specified by the name
// In this case it should be FieldOptions<IRole>[]
// But right now it is FieldOptions<IRole | IAddress[] | string>[]
// I just need a way to narrow down to what it should be
description: 'This is the name on the role'
}
]
}
Currently, my FieldOptions interface looks like this, which I know is incorrect.
interface FieldOptions<T, K extends keyof T> {
name: K;
description?: string;
children?: FieldOptions<T[K], keyof T[K]>[];
}
I've been searching for examples similar to mine, but most involve known keys and conditional properties. My scenario is different as it relies on unknowns until input from the user provides a type. Any insights or assistance would be greatly appreciated. Thank you!