我有以下OrganisationInterface
类型:
export declare interface OrganisationInterface {
documents?: {
[documentType: OrganisationDocumentTypesList]: { // enum
id: string;
name: string;
src?: string;
}
};
}
在我的可执行代码中,我有以下代码:
const organisation = { ...<OrganisationInterface>organisationSnap.data(), id: organisationId }
// Attempt 1 - Have typescript infer the type of 'record' variable
// TS throws error: 'record' is of type 'unknown'
const documentRecord = Object.values(organisation?.documents ?? {}).find((record) => record.id! === fileId)
// Attempt 2 - Cast 'record'
// TS throws error: No overload matches this call
const documentRecord = Object.values(organisation?.documents ?? {}).find((<OrganisationInterface['documents']>record) => record.id! === fileId)
// Attempt 3 - Cast 'record' variable on right side of 'find' method
// TS throws error - 'documents' is possibly undefined
const documentRecord = Object.values(organisation?.documents ?? {}).find((record) => (<OrganisationInterface['documents']>record).id! === fileId)
// Attempt 4 - Change 'documents' subobject from optional to required
// TS throws error - Property 'id' does not exist on type '{}'
const documentRecord = Object.values(organisation?.documents ?? {}).find((record) => (<Required<OrganisationInterface>['documents']>record).id! === fileId)
为什么TS拒绝让我分配通过Array.find()
函数返回的record
变量的类型?
1条答案
按热度按时间idfiyjo81#
当你看到
Object.values
的定义时:values<T>(o: { [s: string]: T } | ArrayLike<T>): T[];
您可以看到类型是推断的。可以为索引类型设置别名:
那么你可以传递这个返回类型:
Playground