contentlayerdev / contentlayerdev/contentlayer

overwrite `field` data with `computedFields` values

Offen
#398 2 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
meta: never-stale needs-research
Vorherrschende Sprache
TypeScript
Sterne
3.5k
Forks
192
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

When defining a document with a `field` and `computedFields` child item with the same name, the `computedFields` field seems to be appended onto the generated type. Resulting in undesired types for the field.

I would have expected the `computedFields` data to overwrite the `field` data, both the `description` and the `type`.

For reference, I am converting a comma separated string of `tags` into a parsed array via the `computedFields`. Here is an example of my document definition:

```js
export const Blog = defineDocumentType(() => ({
name: "Blog",
filePathPattern: `blog/**/*.md`,
fields: {
tags: {
type: "string",
description: "Comma separated listing of tags",
required: false,
},
},
computedFields: {
tags: {
description: "new description",
type: "list",
of: { type: "string" },
resolve: (item) =>
item?.tags?.split(",")?.map((tag) => tag.trim()) ?? undefined,
},
},
}));
```

Generates these types (simplified for the example):

```ts
/** Document types */
export type Blog = {
/** Comma separated listing of tags */
tags?: string | undefined
/** new description */
tags: list
}
```

Extra note: It also seems like the `computedFields` children do not support `list` types, or it may be a bug?

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Das Issue nennt keine Quelldatei oder keinen Test; beginne mit der Blog defineDocumentType-Reproduktion und verfolge, wie Felder und computedFields zu generierten Typen werden. Überprüfe, ob ein berechnetes Feld die ursprüngliche Beschreibung und den ursprünglichen Typ ersetzt und ob Listentypen unterstützt werden. Die Aufgabe ist abgeschlossen, wenn der generierte Typ das vorgesehene einzelne tags-Feld enthält und es für dieses Beispiel eine Regressionstestabdeckung gibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
content
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
32/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.