remark: remark-parse/lib/index.d.ts imports a module that cannot be referenced

Initial checklist

Affected packages and versions

remark-parse 10.0.1

Link to runnable example

No response

Steps to reproduce

https://github.com/yamachu/remark-types-broken-repro

  1. Create a typescript project with strict type checking ( like this https://github.com/yamachu/remark-types-broken-repro/blob/699e5a1ef6963c6f0c6d06d503b962543ffb8945/tsconfig.json )
  2. add dependencies unified 10.1.2 and remark-parse 10.0.1
  3. write code as below (code: REPRO)
  4. execute npx tsc --noEmit for running type check

REPRO

import { unified } from "unified";
import markdown from "remark-parse";

const processor = unified().use(markdown);

const _ = processor.parse("**This is Markdown**");

Expected behavior

Pass type check without error

Actual behavior

Fail type check with error

Runtime

Node v16

Package manager

yarn 1

OS

macOS

Build and bundle tools

Other (please specify in steps to reproduce)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (10 by maintainers)

Most upvoted comments

You are using the TypeScript compiler options "module": "node16" and "moduleResolution": "node16" (the latter is redundant if the former is specified). Those are the correct TypeScript options to use since TypeScript 4.7, but not all packages are using / supporting this yet, including remark apparently.

remark-parse is doing this correctly:

https://github.com/remarkjs/remark/blob/99179c6745a2173d07d5c3b46bad7df767e1989d/packages/remark-parse/lib/index.js#L3

However, TypeScript generates:

/** @type {import('unified').Plugin<[Options?] | void[], string, Root>} */
export default function remarkParse(options: void | import("mdast-util-from-markdown/lib").Options | undefined): void;
export type Root = import('mdast').Root;
export type Options = import('mdast-util-from-markdown').Options;

If we use "module": "node16", it’s even worse.

/** @type {import('unified').Plugin<[Options?] | void[], string, Root>} */
export default function remarkParse(options: void | import("../../../node_modules/mdast-util-from-markdown/lib/index.js").Options | undefined): void;
export type Root = import('mdast').Root;
export type Options = import('mdast-util-from-markdown').Options;

This appears to be a TypeScript bug (causing bugs in our output. This is definitely a bug in remark’s published packages).

I then solved this problem with the following settings.

tsconfig.json

    "baseUrl": "./",                                  /* Specify the base directory to resolve non-relative module names. */
    "paths": {
      "mdast-util-from-markdown/lib": [
        "node_modules/mdast-util-from-markdown/lib/index.d.ts"
      ]
    },   

@yamachu unfortunately the only workaround / solution I see so far is to use "skipLibCheck": true.

thanks @JounQin

here is error log

# ~/Projects/github.com/yamachu/remark-types-broken-repro $ [master]
 yarn run typecheck
yarn run v1.22.19
$ tsc --noEmit
node_modules/remark-parse/lib/index.d.ts:3:26 - error TS2307: Cannot find module 'mdast-util-from-markdown/lib' or its corresponding type declarations.

3   options: void | import('mdast-util-from-markdown/lib').Options | undefined
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in node_modules/remark-parse/lib/index.d.ts:3

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.