TypeError: "not a constructor" when using TypeScript
See original GitHub issueI 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'
});
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Why Does This Typescript Output "[Class] is not a constructor."?
This error message means that [Class] is not initialized by the time a call to its constructor is ...
Read more >TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >TypeError: "X" is not a constructor in JavaScript | bobbyhadz
To solve the "TypeError: 'X' is not a constructor" in JavaScript, make sure to only use the new operator on valid constructors, e.g....
Read more >Resolving TypeError: "X" is Not a Constructor in JavaScript
JavaScript "TypeError: "x" is not a constructor" errors occur when invalid objects or a variable is erroneously used as a constructor.
Read more >Typescript error "class is not a constructor" #8910 - GitHub
I am running the following typescript code in the ES6 target environment and it says that "Cars is not a constructor".
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To be clear, there are two necessary changes from the sample code in the comment at the top of index.d.ts:
// @ts-ignore
above the call to create anew Twitter
In my blank project I also had to either add
"esModuleInterop": true
to thecompilerOptions
in tsconfig.json, or change the import statement toimport * as Twitter from 'twitter-lite'
Then the code ran.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
) isIf
esModuleInterop
is enabled, you can also use theimport ... from
signature, optionally with typings:Note that the
const ... require
signature doesn’t allow you to import typings from twitter-lite, so I would recommend usingesModuleInterop
in any event.