【发布时间】:2023-04-07 05:43:01
【问题描述】:
我不确定这是否可行,但我希望能够采用一个界面并为该界面提供额外的选项。
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'
}
]
}
现在我有我的 FieldOptions 接口,我知道这是错误的。
interface FieldOptions<T, K extends keyof T> {
name: K;
description?: string;
children?: FieldOptions<T[K], keyof T[K]>[];
}
我一直在尝试找到一个示例,但是我发现的所有示例都与已知键有关,并将为此使用条件属性。我的有点不同,因为它建立在未知之上,直到用户输入类型。感谢您为我提供的任何帮助。
【问题讨论】:
-
我正在努力。您需要使用mapped types。递归使它有点棘手。
标签: typescript typescript-typings typescript-generics