【发布时间】:2021-07-05 21:28:13
【问题描述】:
export type Parser = NumberParser | StringParser;
type NumberParser = (input: string) => number | DiplomacyError;
type StringParser = (input: string) => string | DiplomacyError;
export interface Schema {
[key: string]: Parser | Schema;
}
export type RawType<T extends Schema> = {
[Property in keyof T]: T[Property] extends Schema
? RawType<T[Property]>
: ReturnType<Exclude<T[Property], Schema>>;
};
// PersonSchema is compliant the Schema interface, as well as the address property
const PersonSchema = {
age: DT.Integer(DT.isNonNegative),
address: {
street: DT.String(),
},
};
type Person = DT.RawType<typeof PersonSchema>;
可悲的是type Person 被推断为:
type Person = {
age: number | DT.DiplomacyError;
address: DT.RawType<{
street: StringParser;
}>;
}
相反,我希望得到:
type Person = {
age: number | DT.DiplomacyError;
address: {
street: string | DT.DiplomacyError;
};
}
我错过了什么?
【问题讨论】:
-
看起来是同一类型;只是显示不同。当我有机会时,我会验证。有一些方法可以说服编译器扩展类型……因为我在移动设备上,所以现在还没有真正准备好回答。
-
将字符串分配给 address.street 确实有效
标签: typescript typescript-generics conditional-types nested-generics