I would like to create a type so that it will consist of InputKeys as the keys, and the values will be key/value pairs, keys being the corresponding InputFieldKey, and value is string.
Below is what I have so far. But {[key: string]: string}
the key in this context needs to be corresponding field of the main key.
Essentially there is the parent and child relationship constructed with <Record,
the parent part is ok. But I can't figure out the child part which needs to be parent / child again.
There could be as many or as little ???InputFieldKeys
and those keys can have many fields not just 3 (firstName
, lastName
, ...
, ...
)
I am free to modify the code as necessary to create the needed structure, and/or use generics if that helps.
type InputKeys = 'user' | 'building'
type UserInputFieldKey = 'firstName' | 'lastName' | 'age'
type AddressInputFieldKey = 'line1' | 'line2' | 'zip'
type InputError = Record<InputKeys, {[key: string]: string}>
const errors: InputError = {
user: {
firstName: 'First name must be entered',
},
address: {},
}
Any help is appreciated.
type InputError = Record<InputKeys, {[key in UserInputFieldKey]: string}>
?