I'm working with the Airtable API and TypeScript to retrieve records and map them to a custom TypeScript interface. However, I'm encountering a TypeError when I attempt to map the records. Here's the code snippet that's causing the error:
type VolunteerRoleBasicInfo = {
id: string
role: string
key: string
}
async getAllActiveRoles(): Promise<VolunteerRoleBasicInfo[]> {
try {
const records = await this.volunteerRolesTable
.select({
view: 'Grid view',
})
.all()
const activeRoles = records.filter(
(record) => record.get('Status') === 'Active'
)
const volunteerRolesData = activeRoles.map((record) => {
return {
id: record.get('id'),
role: record.get('Role'),
key: record.get('Key'),
}
})
return volunteerRolesData
} catch (err) {
console.log(err)
throw err
}
}
The error I'm getting seems to indicate that it is still expecting a value that could be any number of types when I feel that I've explicitly typed all three properties as a string. Additionally, I know that the field type is string for all these properties on the airtable end. If I wrap the properties in a String() object the code works however, this causes issues and verbose code in a more complex method of the same class. I'm still familiarizing myself with TypeScript, so it is likely a fundamental gap in my understanding of working with types, however, searching through airtable documentation, message boards, and consulting with chatbots has yet to show me that gap! Any insight would be appreciated. Thanks!
Type '{ id: string | number | boolean | Collaborator | readonly Collaborator[] | readonly string[] | readonly Attachment[]; role: string | number | boolean | Collaborator | readonly Collaborator[] | readonly string[] | readonly Attachment[]; key: string | ... 5 more ... | readonly Attachment[]; }[]' is not assignable to type 'VolunteerRoleBasicInfo[]'. Type '{ id: string | number | boolean | Collaborator | readonly Collaborator[] | readonly string[] | readonly Attachment[]; role: string | number | boolean | Collaborator | readonly Collaborator[] | readonly string[] | readonly Attachment[]; key: string | ... 5 more ... | readonly Attachment[]; }' is not assignable to type 'VolunteerRoleBasicInfo'. Types of property 'id' are incompatible. Type 'string | number | boolean | Collaborator | readonly Collaborator[] | readonly string[] | readonly Attachment[]' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'.ts(2322)