contentlayerdev / contentlayerdev/contentlayer

Make it easier to share field definitions

Abierto
#147 1 comentario 1 reacción 0 asignados Ver en GitHub
meta: never-stale topic: typescript
Lenguaje dominante
TypeScript
Estrellas
3.5k
Forks
192
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

```ts
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:

```txt
(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[]; 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.

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Comienza en la API defineDocumentType y localiza en Contentlayer las definiciones de tipos o exportaciones de FieldDef y ReferencePolymorphicFieldDef. Comprueba cómo se infieren sharedFields.title y sharedFields.url y, después, determina si las definiciones de campos reutilizables pueden pasar el type-check y qué enfoque recomendado en userland debería documentarse.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
developer-experience
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
42/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.