Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Make it easier to share field definitions

未關閉
#147 1 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
42/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
typescript

研究方向

從 defineDocumentType API 開始,在 Contentlayer 中找出 FieldDef 和 ReferencePolymorphicFieldDef 的型別定義或匯出。檢查 sharedFields.title 和 sharedFields.url 是如何被推導的,接著判斷可重複使用的欄位定義是否能通過 type-check,以及應記錄哪種建議的 userland 作法。

由索引模型根據 Issue 內容生成。

描述

meta: never-stale topic: typescript

I'd love to be able to do something like this:

const sharedFields = {
  title: (opts) => {
    return {
      type: "string",
      required: true,
      ...opts
    }
  },
  url: {
    type: "string",
    resolve: (doc) => doc._raw.flattenedPath,
  }
}

export const Tag = defineDocumentType(() => ({
  name: "Tag",
  filePathPattern: `tags/**/*.md`,
  fields: {
    title: sharedFields.title,
    color: {
      type: "string",
      description: "Color of the badge to show for the tag",
    },
  },
  computedFields: {
    url: sharedFields.url,
  },
}));

The types from defineDocumentType are inferred by Contentlayer. But this process doesn't work because the correct types cannot be inferred from sharedFields.

For example, title throws the following:

(property) title: (opts: any) => any

Type '(opts: any) => any' is not assignable to type 'FieldDef'.
  Type '(opts: any) => any' is not assignable to type 'ReferencePolymorphicFieldDef'.
    Type '(opts: any) => any' is missing the following properties from type '{ type: "reference"; default?: string; of: readonly DocumentType<string>[]; typeField?: string; }': type, of

I'm assuming this is just a matter of bringing in the proper types from Contentlayer. I poked around for a bit, but couldn't find the right import path containing FieldDef and ReferencePolymorphicFieldDef.

I think there are two parts to this:

  1. Get it to work by importing types from Contentlayer.
  2. Considering these types to be userland code, let's find a cleaner recommended implementation approach for devs.
主要語言
TypeScript
星號
3.5k
分支
192
PR 合併指標
30 天內沒有已合併 PR

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

contentlayerdev/contentlayer 的其他 Issue

查看 contentlayerdev/contentlayer 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。