siefkenj / unified-latex

Utilities for parsing and manipulating LaTeX ASTs with the Unified.js framework
MIT License
85 stars 20 forks source link

Cannot import any types in unified-latex-types with `"module": "Node16"` #72

Open pddg opened 6 months ago

pddg commented 6 months ago

What happened?

I tried to import some types from @unified-latex/unified-latex-types in my TypeScript project whose tsconfig has "module": "Node16", however tsc said Namespace '"/path/to/node_modules/@unified-latex/unified-latex-types/index"' has no exported member 'XXX'.

Why this happened?

tsc reports following errors:

node_modules/@unified-latex/unified-latex-types/index.d.ts:1:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

1 export * from "./libs/ast-types";
                ~~~~~~~~~~~~~~~~~~

node_modules/@unified-latex/unified-latex-types/index.d.ts:2:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

2 export * from "./libs/type-guard";
                ~~~~~~~~~~~~~~~~~~~

node_modules/@unified-latex/unified-latex-types/index.d.ts:3:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

3 export * from "./libs/info-specs";

node16 and nodenext do not support importing the extensionless relative path feature. https://www.typescriptlang.org/docs/handbook/modules/reference.html#node16-nodenext-1

Environment

How to reproduce

mkdir repro
cd repro

# Create package.json
cat << EOF > package.json
{
  "name": "unified-latex-import-issue",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "build": "tsc"
  },
  "devDependencies": {
    "@unified-latex/unified-latex-types": "1.6.0",
    "typescript": "5.3.3"
  }
}
EOF

# Create tsconfig.json with `module: Node16`
cat << EOF > tsconfig.json
{
    "compilerOptions": {
        "target": "ESNext",
        "module": "Node16",
        "moduleResolution": "Node16",
        "outDir": "./dist",
    },
    "exclude": ["node_modules"],
    "include": ["**/*.ts"]
}
EOF

# Add sample script to compile
cat << EOF > main.ts
import type * as Ast from "@unified-latex/unified-latex-types";

function isRoot(node: Ast.Node): node is Ast.Root {
  return node.type === "root";
}
EOF

# Install depends and try to compile with tsc
npm install
npm run build

Workaround

Currently, there is no workaround for this issue. It cannot be used in the project whose tsconfig specified "module": "node16" or "module": "nodenext".

siefkenj commented 6 months ago

I will think about this one. For now, you should be able to use moduleResolution: "Bundler" to import the types.

siefkenj commented 1 week ago

@pddg Is this issue resolved?