This is the object
export const fieldMapping = {
'IP': { name: 'ipAddress', type: AirtableFieldType.STRING },
'IP Country': { name: 'country', type: AirtableFieldType.STRING },
'IP City': { name: 'city', type: AirtableFieldType.STRING },
'IP Continent': { name: 'continent', type: AirtableFieldType.STRING },
'Payment Processor': {
name: 'paymentProcessor',
type: AirtableFieldType.STRING,
},
};
the goal is to get the following type
type fieldNames = | 'ipAddress' | 'country' | 'city' | 'continent' | 'paymentProcessor';
any help is appreciated
>Solution :
One way would be casting it as const
.
const AirtableFieldType = {
STRING: 'sample'
}
export const fieldMapping = {
'IP': { name: 'ipAddress', type: AirtableFieldType.STRING },
'IP Country': { name: 'country', type: AirtableFieldType.STRING },
'IP City': { name: 'city', type: AirtableFieldType.STRING },
'IP Continent': { name: 'continent', type: AirtableFieldType.STRING },
'Payment Processor': {
name: 'paymentProcessor',
type: AirtableFieldType.STRING,
},
} as const;
type FieldKeys = keyof typeof fieldMapping // get all the keys of fieldMapping
type FieldMapping = (typeof fieldMapping)[FieldKeys]['name'] // 'ipAddress' | 'country' | 'city' | 'continent' | 'paymentProcessor'
Demo TypeScript: