Use the $schema
property to narrow down the type of the metadata:
const metadata: PublicationMetadata = ...
if (metadata.$schema === PublicationSchemaId.ARTICLE_LATEST) {
// metadata is ArticleMetadata
metadata.content; // => always string, not undefined
}
Use the $schema
property to determine the type of the metadata in an exhaustive switch statement:
const metadata: PublicationMetadata = ...
switch (metadata.$schema) {
case PublicationSchemaId.ARTICLE_LATEST:
// metadata is ArticleMetadata
break;
case PublicationSchemaId.AUDIO_LATEST:
// metadata is AudioMetadata
break;
case PublicationSchemaId.IMAGE_LATEST:
// metadata is ImageMetadata
break;
case PublicationSchemaId.TEXT_ONLY_LATEST:
// metadata is TextOnlyMetadata
break;
// ...
}
PublicationMetadata
is a discriminated union of all primary publication metadata.