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

  1. changed the title [-]document which field types are allowed for computed fields[/-] [+]Incorrect typing for `enum` on computed fields[/+] on Jun 3, 2022
  2. seancdavis commented on Jun 3, 2022

    @seancdavis
    Collaborator

    Documentation task moved to contentlayerdev/website#62.

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

  3. stale commented on Aug 2, 2022

    @stale

    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.

  4. added this to the Pre-1.0 milestone on Aug 2, 2022
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