markedjs / marked

A markdown parser and compiler. Built for speed.
https://marked.js.org
Other
32.32k stars 3.36k forks source link

Types for `this.parser` don't match notes of v13 release because `parser` is omitted #3326

Closed cascornelissen closed 3 weeks ago

cascornelissen commented 3 weeks ago

Marked version: 13.0.0

Describe the bug A clear and concise description of what the bug is.

To Reproduce Trying to migrate an extension by following the notes of the release results in code that's incompatible with the TypeScript types. The cause seems to be that RendererObject refers to RendererApi which is defined as Omit<_Renderer, "constructor" | "options" | "parser"> so parser is being omitted?

const extension: MarkedExtension = { // Added the explicit type here
  useNewRenderer: true,
  renderer: {
    heading(token) {
      // increase depth by 1
      const text = this.parser.parseInline(token.tokens);
      const level = token.depth;
      return `<h${level + 1}>${text}</h${level + 1}>`;
    }
  }
};

marked.use(extension);
image

Expected behavior The example works as expected.

UziTech commented 3 weeks ago

That is because the this that is passed in is a Renderer not a RendererObject.

The function signature in typescript should be

heading(this: Renderer, token: Tokens.Heading): string {
cascornelissen commented 3 weeks ago

Based on the works as intended label I assume not, but is it not possible to type this correctly on the side of marked?

UziTech commented 3 weeks ago

You are creating the function so you need to type it correctly. The function signature I provided is the way you would create the correctly typed function.