Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Make it easier to share field definitions

Đang mở
#147 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
42/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
developer-experience

Hướng nghiên cứu

Bắt đầu từ API defineDocumentType và tìm các định nghĩa kiểu hoặc export của FieldDef và ReferencePolymorphicFieldDef trong Contentlayer. Kiểm tra cách suy luận kiểu của sharedFields.title và sharedFields.url, sau đó xác định liệu các định nghĩa field có thể tái sử dụng có thể vượt qua type-check hay không và nên tài liệu hóa cách tiếp cận userland được khuyến nghị nào.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
TypeScript
Star
3.5k
Fork
192
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của contentlayerdev/contentlayer

Tất cả issue của contentlayerdev/contentlayer

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.