I have 2 kinds of Frontmatter
:
FrontmatterTaxonomy
, which is an object withkind: "taxonomy"
and ataxnomyName
that is astring
.FrontmatterContent
, which is an object withkind: "content"
that should also allow any other keys, as long as their values arestring[]
.
I'm not sure how to model this - so far, I tried it rather simple, which does not work because "content"
is not of type "TaxonomyTerms"
.
How could I achieve this?
type TaxonomyTerms = string[]
type TaxonomyData = Record<string, TaxonomyTerms>
// alternative, doesn't work either
// type TaxonomyData = { [taxonomyTerm: string]: string[] }
type FrontmatterContent = TaxonomyData & {
kind: 'content'
}
interface FrontmatterTaxonomy {
kind: 'taxonomy'
taxonomyName: string
}
type Frontmatter = FrontmatterContent | FrontmatterTaxonomy
// Doesn't work.
// Type 'string' is not assignable to type 'TaxonomyTerms'
const fm1: Frontmatter = {
kind: 'content',
tags: ['foo', 'bar']
}
// Works.
const fm2: Frontmatter = {
kind: 'taxonomy',
taxonomyName: 'categories'
}