Skip to content

Type error when extracting computedFields out from the defineDocumentType function as a const #348

@Itsnotaka

Description

@Itsnotaka
/** @type {import('contentlayer/source-files').ComputedFields} */
const computedFields = {
  slug: {
    type: "string",
    resolve: (doc: { _raw: { flattenedPath: never } }) =>
      `/${doc._raw.flattenedPath}`,
  },
  slugAsParams: {
    type: "string",
    resolve: (doc: { _raw: { flattenedPath: string } }) =>
      doc._raw.flattenedPath.split("/").slice(1).join("/"),
  },
};

export const Posts = defineDocumentType(() => ({
  name: "Posts",
  filePathPattern: `posts/**/*.mdx`,
  contentType: "mdx",
  fields: {
    title: {
      type: "string",
      required: true,
    },
    description: {
      type: "string",
    },
    date: {
      type: "date",
      required: true,
    },
    published: {
      type: "boolean",
      default: true,
    },
    image: {
      type: "string",
      required: true,
    },
    visibility: {
      type: "string",
      required: true,
    },
    authors: {
      // Reference types are not embedded.
      // Until this is fixed, we can use a simple list.
      // type: "reference",
      // of: Author,
      type: "list",
      of: { type: "string" },
      required: true,
    },
  },
  // @ts-expect-error TODO: it's conflict with extracted computedFields (This would introduce the error)
  computedFields,
}));

By using the code of the above in contentlayer.config.ts it gives type error. However putting the constant object back fixes this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions