Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
yamachu opened this issue Sep 7, 2022 · 15 comments
Closed
4 tasks done
Labels
👀 no/external This makes more sense somewhere else 👎 phase/no Post cannot or will not be acted on

Comments

@yamachu
Copy link

yamachu commented Sep 7, 2022

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)

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Sep 7, 2022
@JounQin
Copy link
Member

JounQin commented Sep 7, 2022

Can you paste some error logs?

@yamachu
Copy link
Author

yamachu commented Sep 7, 2022

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.

@JounQin
Copy link
Member

JounQin commented Sep 7, 2022

There is indeed a lib/index.d.ts for mdast-util-from-markdown, maybe you have different and incompatible mdast-util-from-markdown versions installed.

https://unpkg.com/browse/[email protected]/lib/index.d.ts

I'll check your reproduction soon.

@JounQin
Copy link
Member

JounQin commented Sep 7, 2022

OK, it should be related to exports field from mdast-util-from-markdown.

@yamachu
Copy link
Author

yamachu commented Sep 7, 2022

yes, it's related to exports.

syntax-tree/mdast-util-from-markdown#29
This Issue seems reluctant to add to export.

@yamachu
Copy link
Author

yamachu commented Sep 7, 2022

https://github.com/yamachu/remark/tree/fix-lib-types

With these changes, it worked in my environment.
I have searched a bit for a compile option to see if it is possible to make the import path less deep, but have not found it yet...

@remcohaszing
Copy link
Member

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:

* @typedef {import('mdast-util-from-markdown').Options} Options

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).

@wooorm
Copy link
Member

wooorm commented Sep 7, 2022

@remcohaszing I remember there was a similar issue somewhere, and that it was also reported (by you?) over at TypeScript?

@remcohaszing
Copy link
Member

Nope, doesn't ring a bell.

@wooorm
Copy link
Member

wooorm commented Sep 7, 2022

OK well I am not sure what to search for but I am pretty sure you commented on exactly this issue in one of the TypeScript repos and linked it

@remcohaszing
Copy link
Member

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

@yamachu
Copy link
Author

yamachu commented Sep 7, 2022

related? microsoft/TypeScript#38111

@remcohaszing
Copy link
Member

I think that’s the issue causing this indeed. Hard to believe such a critical issue has been open for over 2 years 😕

@wooorm wooorm added the 👀 no/external This makes more sense somewhere else label Sep 7, 2022
@wooorm
Copy link
Member

wooorm commented Sep 7, 2022

Closing, see #1040 (comment)

@wooorm wooorm closed this as not planned Won't fix, can't repro, duplicate, stale Sep 7, 2022
@wooorm wooorm added the 👎 phase/no Post cannot or will not be acted on label Sep 7, 2022
@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Sep 7, 2022
@yamachu
Copy link
Author

yamachu commented Oct 3, 2022

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"
      ]
    },   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 no/external This makes more sense somewhere else 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

4 participants