contentlayerdev / contentlayer

Contentlayer turns your content into data - making it super easy to import MD(X) and CMS content in your app
https://www.contentlayer.dev
MIT License
3.27k stars 200 forks source link

Incorrect typing for `enum` on computed fields #241

Open stefanprobst opened 2 years ago

stefanprobst commented 2 years ago

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 https://github.com/contentlayerdev/contentlayer/blob/dcc48858f7643f882a72a9cc5941deb36bbb1ecf/packages/%40contentlayer/core/src/generation/generate-types.ts#L120


steps to reproduce:

seancdavis commented 2 years ago

Documentation task moved to contentlayerdev/website#62.

I changed the title to reflect the CL-specific issue. cc @schickling

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.