I have this sort of type :
type Schema = {
name: {
type: string
}
age: {
type: number
}
}
I would like to have a type which flatten it.
I expect this:
type MyType<Schema> = {
name: string
age: number
}
But my try to express that doesn't succeed :
type UIType<Schema> = {
[Key in keyof Schema]: Schema[Key].type
}
The error thrown is "Member 'type' implicitly has an 'any' type.(7008)".
CodePudding user response:
If you define a type:
type ObjTypeFromSchema<S extends Record<string, { type: any }>> = {
[K in keyof S]: S[K]["type"]
}
then you should be able to throw any schema that conforms to Record<string, { type: any }>
at it.
In your case
type O = ObjTypeFromSchema<Schema>;
gives a type for O
of:
{
name: string;
age: number;
}