Skip to content

Add Generic Data Typing for matter Frontmatter Parsing #168

Open
@emadbaqeri

Description

The matter function currently returns a GrayMatterFile<string>, which loses type information about the expected data shape.

It would be better to allow passing a generic type parameter for the data property, like this:

interface PostMetadata {
  title: string;
  description: string;
}

declare function matter<T>(content: string): GrayMatterFile<string, T>;

const file = matter<PostMetadata>(`markdown string`); 

file.data.title; // typed as string

This way consumer code could get type safety when accessing the metadata.

For example in my case I'm parsing MDX files that expect title and description in the frontmatter. With generic data types I could get code completion and validation for those fields.

Potentially the GrayMatterFile type could be updated to add a second generic parameter:

interface GrayMatterFile<TContent, TData> {
  content: TContent;
  data: TData;   
  // ...
}

Please let me know your idea, and even let me know if I can do this in the current version of the lovely gray-matter.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions