Skip to content

type assertion seems not working in ts #2

Closed
@JounQin

Description

@JounQin

Initial checklist

Problem

if (isElement(parent)) {
  console.log(parent) // expect `Element` type here
}

Solution

remove https://github.com/syntax-tree/hast-util-is-element/blob/main/index.js#L60

So that it will yield the following typings

/**
 * Check if a node is an element and passes a certain node test
 *
 * @template {Element} Y
 * @callback AssertPredicate
 * @param {unknown} [node]
 * @param {number|null|undefined} [index]
 * @param {Parent|null|undefined} [parent]
 * @returns {node is Y}
 */
export const isElement: (<T extends import('hast').Element>(
  node?: unknown,
  test?:
    | T['tagName']
    | TestFunctionPredicate<T>
    | (T['tagName'] | TestFunctionPredicate<T>)[],
  index?: number | undefined,
  parent?:
    | import('unist').Parent<
        import('unist').Node<import('unist').Data>,
        import('unist').Data
      >
    | undefined,
  context?: unknown
) => node is T)

Alternatives

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions