rehype-pretty / rehype-pretty-code

Beautiful code blocks for Markdown or MDX.

Home Page:https://rehype-pretty.pages.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect types

o-az opened this issue · comments

commented
Property 'children' does not exist on type 'Node<Data>'.ts

The Node type is the following (from hast which exports it from uinst):

export interface Node<TData extends object = Data> {
    type: string;
    data?: TData | undefined;
    position?: Position | undefined;
}

In here the usage example has node.children and node.properties but neither children nor properties are properties of type Node.

I think it should be Element not Node :(

commented

@atomiks I think I found another issue.

Property 'properties' does not exist on type 'ElementContent'.
  Property 'properties' does not exist on type 'Comment'.ts(2339)

This is happening in child.properties.style:

CleanShot 2023-06-13 at 01 16 32@2x

The Element['children'] type is ElementContent[] and ElementContent type is:

type ElementContent = ElementContentMap[keyof ElementContentMap];

and ElementContentMap type doesn't have properties property.

Thank you for your attention. Consider adding a linting step in CI. I would be happy to contribute that!

Yeah that would definitely be the best idea. Prior to 0.9.6 they just had any types, but adding the stricter types requires a lot more work/testing to make sure everything's good.

I believe this is expected, as in this case you need to use a type guard to verify that the child is an Element node, and not something like a Text node. Something like this works:

import type {Element} from 'hast';

function isElement(value: any): value is Element {
  return value ? value.type === 'element' : false;
}