Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect typing for enum on computed fields #241

Open
stefanprobst opened this issue Jun 1, 2022 · 2 comments
Open

Incorrect typing for enum on computed fields #241

stefanprobst opened this issue Jun 1, 2022 · 2 comments
Labels
bug Something isn't working meta: never-stale
Milestone

Comments

@stefanprobst
Copy link
Contributor

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 [email protected]:stefanprobst/issue-contentlayer-computed-field-types.git
  • cd issue-contentlayer-computed-field-types
  • npm i
  • npm run generate
  • cat .contentlayer/generated/types.d.ts
@seancdavis seancdavis changed the title document which field types are allowed for computed fields Incorrect typing for enum on computed fields Jun 3, 2022
@seancdavis seancdavis added the bug Something isn't working label Jun 3, 2022
@seancdavis
Copy link
Collaborator

Documentation task moved to contentlayerdev/website#62.

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

@stale
Copy link

stale bot commented Aug 2, 2022

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.

@stale stale bot added the meta: stale label Aug 2, 2022
@schickling schickling added this to the Pre-1.0 milestone Aug 2, 2022
@stale stale bot closed this as completed Aug 12, 2022
@schickling schickling reopened this Aug 14, 2022
@stale stale bot removed the meta: stale label Aug 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working meta: never-stale
Projects
None yet
Development

No branches or pull requests

3 participants