I'm trying to create a number of types to be able to write:
- MyObjectDetails — defines MyObject structure
- MyObject - object that correctly follows these MyObjectDetails.
// Details example
const testObjectDetails: MyObjectDetails = {
name: 'Test',
fields: [
{
name: 'property1'
},
{
name: 'property2'
}
]
}
// Object example
const testObject: MyObject<typeof testObjectDetails> = {
property1: 123,
property3: 456 // Should throw Error -> no such field in the details
// Should throw Error -> property2 is missing
}
I wrote some code, but they it's not working as expected:
type MyField = string extends infer T ? Readonly<{
name: T
}> : never
type MyObjectDetails = Readonly<{
name: string,
fields: ReadonlyArray<MyField>
}>
type MyObject<T extends MyObjectDetails> = {
[K in T['fields'][number]['name']]: any;
}
Can you help me?