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

TypeError: "not a constructor" when using TypeScript #83

Closed
danbockapps opened this issue Apr 23, 2020 · 7 comments · Fixed by #88
Closed

TypeError: "not a constructor" when using TypeScript #83

danbockapps opened this issue Apr 23, 2020 · 7 comments · Fixed by #88

Comments

@danbockapps
Copy link

I get this error when using the example code at the top of index.d.ts:

Users-MacBook-Pro:twitter-lite-test danbock$ ts-node dev.ts 

/Users/danbock/code/twitter-lite-test/dev.ts:3
const twitter = new Twitter({
                ^
TypeError: twitter_lite_1.Twitter is not a constructor
    at Object.<anonymous> (/Users/danbock/code/twitter-lite-test/dev.ts:3:17)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Module.m._compile (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/index.ts:836:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/index.ts:839:12)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at main (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/bin.ts:226:14)
    at Object.<anonymous> (/Users/danbock/.nvm/versions/node/v12.16.2/lib/node_modules/ts-node/src/bin.ts:485:3)

I reproduced this on a blank project. Just ran npm install twitter-lite and copied this code into a blank file and ran it using ts-node. Got the same error.

import { Twitter } from 'twitter-lite';

const twitter = new Twitter({
 consumer_key: 'XYZ',
 consumer_secret: 'XYZ',
 access_token_key: 'XYZ',
 access_token_secret: 'XYZ'
});
@AlberErre
Copy link

Same issue here 🤷🏻‍♂️

I've added // @ts-ignore above that line as a temporary workaround.

@danbockapps
Copy link
Author

@AlberErre Above what line? I believe that TypeScript compilation is actually happening without errors, and the TypeError I'm seeing is a runtime JavaScript error.

@kyleratti
Copy link

@danbockapps I am seeing the same thing on typescript@^3.8.3 and twitter-lite@^0.10.1-0. I think this has to do with the typings exporting the entire namespace while the actual library exports the Twitter class.

I was a bit confused by the suggestion from @AlberErre as well, but I realized he meant he added // @ts-ignore above the client line like so:

import Twitter from "twitter-lite"; // typings and runtime OK for this line

...

// changing your import to match above will trigger 'This expression is not constructable.'
// the @ts-ignore flag will let TS compile anyway
// @ts-ignore
const twitter = new Twitter({
 consumer_key: 'XYZ',
 consumer_secret: 'XYZ',
 access_token_key: 'XYZ',
 access_token_secret: 'XYZ'
});

@danbockapps
Copy link
Author

danbockapps commented Apr 24, 2020

To be clear, there are two necessary changes from the sample code in the comment at the top of index.d.ts:

  1. Remove the curly brackets from the import statement
  2. Add // @ts-ignore above the call to create a new Twitter

In my blank project I also had to either add "esModuleInterop": true to the compilerOptions in tsconfig.json, or change the import statement to import * as Twitter from 'twitter-lite' Then the code ran.

@dylanirlbeck
Copy link
Contributor

Hi all, sorry you're seeing these errors in TypeScript. We just added typings to the library, so there was bound to be some bugs 😄 That said, I'd encourage all of you to take a look to see if you can find the issue and open a PR!

I'm going to cc @fdebijl since he made the PR for types originally. I'm no TS expert so hopefully we can work together to get this fixed before the next release of twitter-lite.

@fdebijl
Copy link
Collaborator

fdebijl commented Apr 24, 2020

I completely overlooked esModuleInterop while developing the typings, I'm submitting a PR with a fix as we speak.

Following this PR, the safe import signature (which will work without esModuleInterop) is

const Twitter = require('twitter-lite')

If esModuleInterop is enabled, you can also use the import ... from signature, optionally with typings:

import Twitter from 'twitter-lite';
import Twitter, { TwitterOptions, AccessTokenOptions } from 'twitter-lite';

Note that the const ... require signature doesn't allow you to import typings from twitter-lite, so I would recommend using esModuleInterop in any event.

@DalderupMaurice
Copy link

DalderupMaurice commented Apr 1, 2021

I've had this issue with version 1.1.0
Had to add esModuleInterop: true to fix the issue, without it gives the error

I assume this PR would fix that: #139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

6 participants