How can you generically construct a type derived from an object following the format below:
export const IMMEDIATE_FAMILY_MEMBERS_LIST_FIELDS = [
{ name: 'Relationship', type: 'dropdown', options: FAMILY_MEMBERS.NAMES },
{ name: 'Full name of family member as shown in passport', type: 'text' },
{
name: 'Country family member lives in',
type: 'dropdown',
options: COUNTRY.NAMES,
},
{
name: 'Immigration status in that country',
type: 'dropdown',
options: IMMIGRATION_STATUS_OPTIONS,
},
] as const
Into this:
export type ImmediateFamilyMembersListType = Array<{
Relationship: FamilyMembersType
'Full name of family member as shown in passport': string
'Country family member lives in': CountryType
'Immigration status in that country': keyof typeof IMMIGRATION_STATUS_OPTIONS
}>
Except without manually typing it every time for every object?
Here's my best attempt, but it's not generic nor does the A extends B ? string[] : string even work:
type ImmediateFamilyMembersListFieldsType = typeof IMMEDIATE_FAMILY_MEMBERS_LIST_FIELDS
type ImmediateFamilyMembersListType = Record<ImmediateFamilyMembersListFieldsType[number]['name'], ImmediateFamilyMembersListFieldsType[number]['type'] extends 'dropdown' ? string[] : string>[]
Instead, it just resolves to:
type ImmediateFamilyMembersListType = Record<"Relationship" | "Full name of family member as shown in passport" | "Country family member lives in" | "Immigration status in that country", string>[]