payload icon indicating copy to clipboard operation
payload copied to clipboard

plugin-seo: Incorrect type for GenerateTitle and other generate functions

Open DanRibbens opened this issue 2 years ago • 1 comments

The GenerateTitle type does not appear to be correct and the readme example doesn't offer type safety: generateTitle: ({ doc }) => `Website.com — ${doc.title.value}`,

I believe it should be changed to:

 export type GenerateTitle<T = any> = (
  args: { doc: T; locale?: string },
) => string | Promise<string>

// useage: 

type Project = {
    title: string
}

const generateTitleFromProject: GenerateTitle<Project> = function generateTitle({ doc }) { return doc.title };

DanRibbens avatar Aug 24 '23 19:08 DanRibbens

The suggested typing wouldn't consider that the actual values passed are Fields though, right?

Mixed and matched code snippet to show what the actual args are:

export type Fields = {
    [path: string]: FormField;
};

export type FormFieldsContext = [Fields, Dispatch<FieldAction>];

declare const useAllFormFields: () => FormFieldsContextType;

const [fields] = useAllFormFields()

const newHref = await generateURL({
  ...docInfo,
  doc: { fields },
  locale,
})

const generatedDescription = await generateDescription({
  ...docInfo,
  doc: { ...fields },
  locale,
})

barthy-koeln avatar Nov 06 '23 18:11 barthy-koeln

Closing as these types have been updated for V3

paulpopus avatar Jun 17 '24 15:06 paulpopus