yargs: @types/typescript does not work with `import` syntax
Reproduce by running the following in an empty directory:
npm init
npm i yargs
echo "import yargs from 'yargs';" > index.ts
tsc index.ts
We get the error
index.ts:1:19 - error TS2307: Cannot find module 'yargs' or its corresponding type declarations.
1 import yargs from 'yargs'
~~~~~~~
Found 1 error.
Reproduced with yargs 16.0.3
and tsc 4.0.2
.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (6 by maintainers)
https://github.com/yargs/yargs#typescript yargs doesn’t ship built-in types yet, so one has to install
@types/yargs
as well.@bcoe you can already
import yargs as 'yargs';
if you haveesModuleInterop
turned on (default intsc --init
) and@types/yargs
in place. We do so in stylable: https://github.com/wix/stylable/blob/master/packages/cli/src/cli.tsWe are getting yargs in its
require
form though, as our modules transpile to commonjs (meaning arequire('yargs')
is being called).EDIT: just to clarify…
esModuleInterop
causesallowSyntheticDefaultImports
to be turned on, which causes the type checker to allow importing “commonjs modules” (.d.ts
saysexport =
) using default import.I apologize for the confusion it created having this briefly work in v15. We do not intend to ship TypeScript definitions in yargs or yargs-parser in the near future.
Here’s a post on the topic.
@types/yargs
is well supported and used widely by the community, but deviates sigificantly from the actual TypeScript API surface of the library.Basically, we had the choice break TypeScript for 1000s of existing libraries, or ship without types ourselves… I think this is the right call.
The other problem with shipping the library itself with the types is that breaking changes to types vs the API surface become indistinguishable.