I think it might be useful to provide an exported generic "base" type that all document types inherit:
// the fields common to all document types
export type Document = {
_id: string;
_raw: RawDocumentData;
type: string;
body: Markdown;
};
Generated user-defined document types could then inherit from this type:
export type Post = Document & {
title: string;
description: string;
tags: string[];
};
This would enable users to more easily write generic functions handling generated document types. Here's an example:
import { Document } from "contentlayer/generated"; // or maybe expose this as a non-generated built-in type
type Tagged = Document & { tags: string[] };
const filterByTag = <T extends Tagged>(documents: T[], tag: string): T[] => {
return documents.filter(doc => doc.tags.includes(tag));
}
This filterByTag function works for any and all generated document types and could be applied if you had, say, Post, Doc, Recipe, and Product types. I've worked around this easily enough by creating my own "base" type, but I personally think that a built-in would provide a nicer DX.
Another benefit would be that resolve functions could be more strongly typed. Here's an example:
computedFields: {
relativeUrl: {
type: 'string',
resolve: (doc: Document) => `/docs/${doc._raw.flattenedPath}`;
}
}
I think it might be useful to provide an exported generic "base" type that all document types inherit:
Generated user-defined document types could then inherit from this type:
This would enable users to more easily write generic functions handling generated document types. Here's an example:
This
filterByTagfunction works for any and all generated document types and could be applied if you had, say,Post,Doc,Recipe, andProducttypes. I've worked around this easily enough by creating my own "base" type, but I personally think that a built-in would provide a nicer DX.Another benefit would be that
resolvefunctions could be more strongly typed. Here's an example: