DefinitelyTyped / DefinitelyTyped

The repository for high quality TypeScript type definitions.
Other
48.73k stars 30.19k forks source link

[@types/argparse] npm packages out of sync: argparse and @types/argparse #65658

Open melonmouse opened 1 year ago

melonmouse commented 1 year ago

tl;dr: should a peerDependency between a package and it's type package be required? That could avoid automatically getting incompatible versions of a package and it's type dependency (like in the example below). From the discussion thread it seemed like this is something that is implementable in DefinitelyTyped (but I don't know if this is the case or not).

Let me know if this issue is appropriate (and my apologies if it's not). Thanks for your amazing work on making typescript better!

Discussed in https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/63645

Originally posted by **melonmouse** December 16, 2022 **The symptoms** Got an unexplained `TypeError: simpleExample.add_argument is not a function` when trying to use argparse in typescript (using an example from the docs). **The cause** The npm package `@types/argparse` defaulted to version `2.0.10` for me, while `argparse` defaulted to `argparse@1.0.10`. These are not compatible and cause errors for any of the functions that [had their names changed from v1 to v2](https://github.com/nodeca/argparse/blob/master/doc/migrate_v1_to_v2.md). Installing the newer version of argparse explicitly fixed it for me: `npm install argparse@2` This went wrong on npm versions 7.11.2 and 9.2.0. **Possible fixes** - upgrade the default of `argparse` to `2.0.10` - downgrade [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) the default version of `@types/argparse` to 1.0.10 - update dependency requirements so npm either installs compatible versions or complains when installing incompatible versions I also filed an issue in the main argparse github repo: https://github.com/nodeca/argparse/issues/172 **p.s.** Thanks for adding types for so many packages! I love typescript and am thankful for your great work!
melonmouse commented 1 year ago

This is a thread about a similar issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/47369

tlaziuk commented 1 year ago

installing argparse and @types/argparse got me versions @types/argparse@^2.0.10 and argparse@^2.0.1 using npm@9.5.1

I have read your other threads, and they are very valid, so assuming the issue exists, is it even possible to make different than the latest version as default?

melonmouse commented 1 year ago

The specific issue in argparse is not current (sorry for not specifying that) - it happened at one point in time, and I'm not sure what was the root cause. All I know is that specific conditions must have existed to cause this for me, and other bugs suggest it is not an entirely unique issue.

One hypothesis: could this be caused by an older version of a package being installed as a dependency (e.g. argparse@1), and then later the user adds the types for that package without specifying the version (e.g. @types/argparse) which is resolved to the newest and incompatible version (e.g. @types/argparse@2). If that is the cause, I suspect adding a peerDependency between a package and its types would at the very least inform the user about the mismatch, instead of getting a runtime error down the line.