Skip to content

Incorrect typing for enum on computed fields #241

Description

@stefanprobst

currently when defining a computed field of type enum like this:

import { defineDocumentType, makeSource } from 'contentlayer/source-files'

const locales = ['en', 'de'] as const

const Post = defineDocumentType(() => {
  return {
    name: 'Post',
    filePathPattern: 'posts/**/*.mdx',
    fields: {
      language: {
        type: 'enum',
        options: locales,
        required: true
      }
    },
    computedFields: {
      locale: {
        type: 'enum',
        options: locales,
        resolve(doc) {
          return doc['language']
        }
      }
    }
  }
})

export default makeSource({
  contentDirPath: 'content',
  documentTypes: [Post]
})

this generates a ts schema:

export type Post = {
  /** File path relative to `contentDirPath` */
  _id: string
  _raw: Local.RawDocumentData
  type: 'Post'
  language: 'en' | 'de'
  /** Markdown file body */
  body: Markdown
  locale: enum
}  

note that the regular enum field is correct, but the computed enum field is of type enum

i guess this is because of

.map((field) => `${field.description ? ` /** ${field.description} */\n` : ''} ${field.name}: ${field.type}`)


steps to reproduce:

  • git clone git@github.com:stefanprobst/issue-contentlayer-computed-field-types.git
  • cd issue-contentlayer-computed-field-types
  • npm i
  • npm run generate
  • cat .contentlayer/generated/types.d.ts

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions