Closed treylorswift closed 3 years ago
+1 Same problem
I can imagine this would be rather frustrating -- I'm tagging @Fdebijl since he integrated TypeScript and might be able to identify a fix for this.
The main module exports a class, so the regular methods of importing a module in TS don't work in every case (which I had to learn the hard way).
The only way to import twitter-lite at this time with esModuleInterop
disabled is const Twitter = require('twitter-lite')
. If you have esModuleInterop
enabled, you may also use the more natural import Twitter from 'twitter-lite'
, which also allows you to import typings like so: import Twitter, { AccessTokenOptions } from 'twitter-lite'
.
Currently this is only explained at the top of index.d.ts
, but we may have to include this explanation in the readme as well to prevent any confusion.
I'm not sure what the problem is but I had a hell of a time trying to integrate twitter-lite into a Typescript node.js project. I have figured out a work-around (jump to the bottom to see it) but want to document how the intuitive approaches to importing are currently failing. My first attempt:
produced a run-time error like this:
So then I tried...
but that gets a compiler error:
So then I tried:
this also gets a compiler error:
I believe this is due to my import seeing 'Twitter' as a namespace, not a class. I get similar errors elsewhere when using 'Typescript' as a type on variable declarations. However if I ignore those warnings by adding a ts-ignore just above the lines where I use 'Twitter'...
If I do it this way, everything just works. But it does mean the TS compiler is effectively not doing any type-checking on the Twitter object (and, auto-complete in VisualStudio stops working). So there seems to be a problem somewhere.. perhaps the typescript definitions?