Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@types/typescript does not work with import syntax #1749

Closed
dalcde opened this issue Sep 10, 2020 · 15 comments
Closed

@types/typescript does not work with import syntax #1749

dalcde opened this issue Sep 10, 2020 · 15 comments

Comments

@dalcde
Copy link

dalcde commented Sep 10, 2020

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.

@AviVahl
Copy link

AviVahl commented Sep 10, 2020

https://github.com/yargs/yargs#typescript
yargs doesn't ship built-in types yet, so one has to install @types/yargs as well.

@dalcde
Copy link
Author

dalcde commented Sep 10, 2020 via email

@AviVahl
Copy link

AviVahl commented Sep 10, 2020

Maintainers are working to convert the entire project to TypeScript: #1586
Work there is probably related. They do write about @types/yargs and the built-in .d.ts

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

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.

@bcoe bcoe closed this as completed Sep 10, 2020
@bcoe bcoe added the wontfix label Sep 10, 2020
@dalcde
Copy link
Author

dalcde commented Sep 10, 2020 via email

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@dalcde the syntax should be:

#!/usr/bin/env node
import yargs = require('yargs');

const argv = yargs.options({
  a: { type: 'boolean', default: false },
  b: { type: 'string', demandOption: true },
  c: { type: 'number', alias: 'chill' },
  d: { type: 'array' },
  e: { type: 'count' },
  f: { choices: ['1', '2', '3'] }
}).argv;

I believe to match the @types/yargs definitions.

@bcoe bcoe added the bug label Sep 10, 2020
@bcoe bcoe reopened this Sep 10, 2020
@bcoe bcoe changed the title Cannot import from typescript @types/typescript does not work with import syntax Sep 10, 2020
@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@dalcde I wonder if, now that we support ESM in yargs, there's a way @types/yargs could be modified to support both require and import syntax.

The reason that = require('yargs'); is necessary, is because the non-ESM API surface of yargs uses module.exports.

@AviVahl
Copy link

AviVahl commented Sep 10, 2020

@bcoe you can already import yargs as 'yargs'; if you have esModuleInterop turned on (default in tsc --init) and @types/yargs in place.
We do so in stylable: https://github.com/wix/stylable/blob/master/packages/cli/src/cli.ts

We are getting yargs in its require form though, as our modules transpile to commonjs (meaning a require('yargs') is being called).

EDIT: just to clarify... esModuleInterop causes allowSyntheticDefaultImports to be turned on, which causes the type checker to allow importing "commonjs modules" (.d.ts says export =) using default import.

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@AviVahl, I wish this was a bit more straightforward for folks ... especially now that the library itself does support ESM. I think there's just room to improve the ESM/TypeScript interop story.

@bcoe
Copy link
Member

bcoe commented Sep 10, 2020

@dalcde if @AviVahl's approach works for you, perhaps we can close this?

@dalcde
Copy link
Author

dalcde commented Sep 11, 2020 via email

@bcoe
Copy link
Member

bcoe commented Sep 11, 2020

@dalcde could you share a minimal repository that is having this issue?

@dalcde
Copy link
Author

dalcde commented Sep 11, 2020 via email

@AviVahl
Copy link

AviVahl commented Sep 11, 2020

Those commands do not include @types/yargs, so you'll get a type error.
Also, they do not include npx tsc --init, so no tsconfig.json was created, and you may be missing other important config (module/moduleResolution)

mkdir yargs-ts-test
cd yargs-ts-test/
npm init -y
npm i yargs
npm i @types/yargs typescript -D
npx tsc --init
echo "import yargs from 'yargs';" > index.ts
npx tsc index.ts 

Works just fine (notice the added npm i @types/yargs typescript -D and npx tsc --init).

@dalcde
Copy link
Author

dalcde commented Sep 11, 2020 via email

@bcoe bcoe closed this as completed Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants