In my codebase, I am dealing with the OrganisationInterface
type:
export declare interface OrganisationInterface {
documents?: {
[documentType: OrganisationDocumentTypesList]: { // enum
id: string;
name: string;
src?: string;
}
};
}
When working in my executable code, this is what I have:
const organisation = { ...<OrganisationInterface>organisationSnap.data(), id: organisationId }
// Here are a few attempts that I made to handle the 'record' variable type inference
const documentRecord1 = Object.values(organisation?.documents ?? {}).find((record) => record.id! === fileId)
const documentRecord2 = Object.values(organisation?.documents ?? {}).find((<OrganisationInterface['documents']>record) => record.id! === fileId)
const documentRecord3 = Object.values(organisation?.documents ?? {}).find((record) => (<OrganisationInterface['documents']>record).id! === fileId)
const documentRecord4 = Object.values(organisation?.documents ?? {}).find((record) => (<Required<OrganisationInterface>['documents']>record).id! === fileId)
I am perplexed as to why TypeScript refuses to allow me to specify the type of the record
variable returned by the Array.find()
function. Any insights?