arethetypeswrong / arethetypeswrong.github.io

Tool for analyzing TypeScript types of npm packages
https://arethetypeswrong.github.io
MIT License
1.18k stars 41 forks source link

lodash types are wrong (or lying?) #131

Closed benasher44 closed 9 months ago

benasher44 commented 9 months ago

I'm not sure 100% what's going on (lodash's js style of exporting is not written in the most standard way — somewhat difficult to parse out in my head), but attw claims lodash's types are correct.

image

However, named lodash imports fail at runtime in node ESM. For example:

import { random } from "lodash";

This will fail with:

SyntaxError: The requested module 'lodash' does not provide an export named 'random'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.10.0
benasher44 commented 9 months ago

Maybe the issue is that the types are correct, but they're lying? If that's the case, feel free to close.

benasher44 commented 9 months ago

I should add that they only fail when running node on ESM output. This import works fine with CJS output (only tested CJS using module set to node, not nodenext)

benasher44 commented 9 months ago

Here's a simple program that illustrates the issue:

import _, { random } from "lodash";
console.log(_.random());
console.log(random());

This compiles fine with tsc --module NodeNext file.ts, but the import fails at runtime (am now wondering if I've discovered a bug with ts emit).

benasher44 commented 9 months ago

Okay so I think I just stumbled upon microsoft/TypeScript#54018. Would it be reasonable to use cjs-module-lexer in this package, and then flag the types with 👺 ?

andrewbranch commented 9 months ago

Duplicate of #35? 😉

benasher44 commented 9 months ago

Oh wow nice!