Use default generics #67

Open
opened 2018-04-15 20:40:43 +00:00 by michaeljota · 3 comments
michaeljota commented 2018-04-15 20:40:43 +00:00 (Migrated from github.com)

Hi there. I'm using this, and I notice that you are using a lot of generics in the Typescript typing. I wonder if you could consider using them with defaults.

  interface GrayMatterOption<
    I extends Input = Input,
    O extends GrayMatterOption<I, O> = GrayMatterOption<I, O>
  > {
    parser?: () => void;
    eval?: boolean;
    excerpt?: boolean | ((input: I, options: O) => string);
    excerpt_separator?: string;
    engines?: {
      [index: string]:
        | ((string) => object)
        | { parse: (string) => object; stringify?: (object) => string };
    };
    language?: string;
    delimiters?: string | [string, string];
  }

I don't actually understand where is the generic being used, and using this would allow to keep the type checking as it is suppose to be without declaring the generics any.


Also, 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 = object, 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 = object> {
    data: T;
    content: string;
    excrept?: string;
    orig: Buffer | I;
    language: string;
    matter: string;
    stringify(lang: string): string;
  }

Let me know what you think about this, I happily would do the PR with the changes.

Hi there. I'm using this, and I notice that you are using a lot of generics in the Typescript typing. I wonder if you could consider using them with defaults. ```typescript interface GrayMatterOption< I extends Input = Input, O extends GrayMatterOption<I, O> = GrayMatterOption<I, O> > { parser?: () => void; eval?: boolean; excerpt?: boolean | ((input: I, options: O) => string); excerpt_separator?: string; engines?: { [index: string]: | ((string) => object) | { parse: (string) => object; stringify?: (object) => string }; }; language?: string; delimiters?: string | [string, string]; } ``` I don't actually understand where is the generic being used, and using this would allow to keep the type checking as it is suppose to be without declaring the generics `any`. --- Also, 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 = object, 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 = object> { data: T; content: string; excrept?: string; orig: Buffer | I; language: string; matter: string; stringify(lang: string): string; } ``` Let me know what you think about this, I happily would do the PR with the changes.
jonschlinkert commented 2018-04-17 20:01:24 +00:00 (Migrated from github.com)

I'm clueless about typescript, it's on my list of things to learn about, but I'm afraid I'll need feedback from others on this one. @onokumus any thoughts?

I'm clueless about typescript, it's on my list of things to learn about, but I'm afraid I'll need feedback from others on this one. @onokumus any thoughts?
robertmassaioli commented 2019-01-16 23:12:20 +00:00 (Migrated from github.com)

I'm okay with adding the default typings, but I'm not okay with changing the type of data to be T, as I mentioned in #69 .

If you want to raise a PR to add in the default generics then I will review and approve it. Cheers.

I'm okay with adding the default typings, but I'm not okay with changing the type of `data` to be `T`, as I mentioned in #69 . If you want to raise a PR to add in the default generics then I will review and approve it. Cheers.
kirgy commented 2023-04-27 21:47:32 +00:00 (Migrated from github.com)

I think this package is dead. Following conventions of type casting from the likes of Axios I've just abstracted this library to support generics like:

import matter from 'gray-matter'

const typedMatter = <T = any>(markdown: string) => {
  const rawMatter = matter(markdown)

  return {
    ...rawMatter,
    data: rawMatter.data as T,
  }
}

export default typedMatter

I think this package is dead. Following conventions of type casting from the likes of Axios I've just abstracted this library to support generics like: ```typescript import matter from 'gray-matter' const typedMatter = <T = any>(markdown: string) => { const rawMatter = matter(markdown) return { ...rawMatter, data: rawMatter.data as T, } } export default typedMatter ```
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#67
No description provided.