Use generics to type data property in GrayMatterFile interface #69

Closed
opened 2018-04-15 22:10:13 +00:00 by michaeljota · 1 comment
michaeljota commented 2018-04-15 22:10:13 +00:00 (Migrated from github.com)

I originally posted this in #67, but I realize that this is actually a bug when using Typescript.

I would like to type the data property in GrayMatterFile interface. With a generic type, of curse. Could be done with this:

  interface GrayMatterFile<T = any, I extends Input = Input> {
    data: T;
    content: string;
    excrept?: string;
    orig: Buffer | I;
    language: string;
    matter: string;
    stringify(lang: string): string;
  }

Using default generics again, allow this to keep backward compatibility, if the T is declared after the I.

  interface GrayMatterFile<I extends Input = Input, T = any> {
    data: T;
    content: string;
    excrept?: string;
    orig: Buffer | I;
    language: string;
    matter: string;
    stringify(lang: string): string;
  }

This is because object does not allow accessing any custom properties, so effectively there are only a couple of properties that you can access when using this with Typescript. By using generics, developers are allowed to shape that property as they probably now the actual model of data property. By using defaults, if the developer does not provide a type, then data will be any, allowing to access any custom property.

I originally posted this in #67, but I realize that this is actually a bug when using Typescript. I would like to type the `data` property in `GrayMatterFile` interface. With a generic type, of curse. Could be done with this: ```typescript interface GrayMatterFile<T = any, I extends Input = Input> { data: T; content: string; excrept?: string; orig: Buffer | I; language: string; matter: string; stringify(lang: string): string; } ``` Using default generics again, allow this to keep backward compatibility, if the `T` is declared after the `I`. ```typescript interface GrayMatterFile<I extends Input = Input, T = any> { data: T; content: string; excrept?: string; orig: Buffer | I; language: string; matter: string; stringify(lang: string): string; } ``` --- This is because `object` does not allow accessing any custom properties, so effectively there are only a couple of properties that you can access when using this with Typescript. By using generics, developers are allowed to shape that property as they probably now the actual model of `data` property. By using defaults, if the developer does not provide a type, then `data` will be `any`, allowing to access any custom property.
robertmassaioli commented 2019-01-16 23:07:43 +00:00 (Migrated from github.com)

In 4.0.2 we have used the type { [key: string]: any; } because without validation that the parsed frontmatter matches the type T, it is probably not a good idea to just cast the read-in type to T. It's just going to result in unexpected errors.

You can add a validation + typecast logic on top of that type if you like.

At any rate, I'm going to close this as "won't fix". Cheers.

In `4.0.2` we have used the type `{ [key: string]: any; }` because without validation that the parsed frontmatter matches the type `T`, it is probably not a good idea to just cast the read-in type to T. It's just going to result in unexpected errors. You can add a validation + typecast logic on top of that type if you like. At any rate, I'm going to close this as "won't fix". Cheers.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rtlabs-llm-agents/gms-gray-matter#69
No description provided.