Skip to content

types for tree do not accept valid unist nodes #16

@binyamin

Description

@binyamin

Initial checklist

Affected package

unist-util-select@4.0.3

Steps to reproduce

Given the following 2 files, install typescript@5.9.3, and run tsc to check the types.

// test.ts
import type { Node } from 'unist';
import { select } from 'unist-util-select';

let input: Node;
const myNode = select('my-node', input)
// tsconfig.json
{
	"compilerOptions": {
		// Only check types
		"noEmit": true,
		// Ignore errors from dependency types
		"skipLibCheck": true,
		// Resolve node modules
		"module": "ESNext",
		"moduleResolution": "bundler"
	}
}

Actual behavior

Typescript rejects unist nodes, even though the function is typed to accept them.

Here is the typescript error, verbatim:

$ tsc
test.ts:5:34 - error TS2345: Argument of type 'Node' is not assignable to parameter of type 'Node<Data> | NodeLike'.
  Type 'Node' is not assignable to type 'NodeLike'.
    Type 'Node' is not assignable to type 'Record<string, unknown>'.
      Index signature for type 'string' is missing in type 'Node'.

5 const myNode = select('my-node', input)
                                   ~~~~~


Found 1 error in test.ts:5

error: "tsc" exited with code 2

Expected behavior

The function should accept any valid unist or unist-like node, including derivatives such as hast and mdast. Curiously, if we replace the "input" variable with u('parent') from unist-builder, typescript is happy.

Runtime

node@v22.17.1

Package manager

npm@11.4.2

Operating system

Fedora Linux 42

Build and bundle tools

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    🤞 phase/openPost is being triaged manually

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions