jashkenas / underscore

JavaScript's utility _ belt
https://underscorejs.org
MIT License
27.29k stars 5.53k forks source link

Modular import cause error in typescript apps #2975

Closed marzzy closed 1 year ago

marzzy commented 1 year ago

As you can see in this Codesand example, I try to use the modular import in a typescript project, it works, but cause the error below:

Cannot find module 'underscore/modules/map' or its corresponding type declarations.ts

jgonggrijp commented 1 year ago

Thank you for reaching out, @marzzy.

I do not see the error you report. Where should I look?

Schermafbeelding 2022-10-14 om 21 29 46

marzzy commented 1 year ago

It takes 2-3 minutes to load fully, after that, the error will appear on the address part like this:

Screenshot 2022-10-15 at 18 07 01
jgonggrijp commented 1 year ago

Thanks, I see it now. The problem is in @types/underscore/package.json, which forbids TypeScript from importing anything other than the monolithic version of the package (exports field):

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/underscore/package.json

Contrast this with Underscore's own package.json, which does allow module imports:

https://github.com/jashkenas/underscore/blob/master/package.json

Best solution is to report a bug to DefinitelyTyped, or to submit a pull request there with your own fix (if you have the patience and courage to work on that).

Easiest solution is to just use the monolithic interface; it's only 8 kB. Second easiest is to put a // @ts-ignore above the import, but this has the disadvantage that TS will not check the type of any function that you import the modular way.

jgonggrijp commented 1 year ago

Closing this as the question seems answered. Please feel welcome to comment again, though.