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

Unable to import module from typescript #14

Open
mleguen opened this issue Oct 15, 2019 · 8 comments
Open

Unable to import module from typescript #14

mleguen opened this issue Oct 15, 2019 · 8 comments

Comments

@mleguen
Copy link

mleguen commented Oct 15, 2019

This is a follow-up of #12

The proposed solution does not work either:

import * as timestamp from 'time-stamp';

leads to :

error TS2349: This expression is not callable.
  Type 'typeof import("[...]/node_modules/time-stamp/index")' has no call signatures.
@mleguen
Copy link
Author

mleguen commented Oct 15, 2019

The only way I found to avoid this error is to add the following line in index.js:

timestamp.default = timestamp;

and to switch back in my code to:

import timestamp from 'time-stamp';

However I can find no way to write an index.d.ts compatible with the current index.js.

@jonschlinkert
Copy link
Owner

want to do a pr?

@mleguen
Copy link
Author

mleguen commented Nov 25, 2019

@jonschlinkert I do not see at the moment how to do it without impacting too much the current behavior of index.js.

@fregante
Copy link

If you use module.exports = foo in your JS file, the TypeScript definition should be export = foo. Example sindresorhus/memoize#32

export default is only for ES Modules and time-stamp isn't one; adding a default export won't change it into one.

TypeScript offers esModuleInterop but it only lets users of a CJS module to import it via import mod from 'module', this module doesn't need any special syntax to make it compatible.

@mleguen
Copy link
Author

mleguen commented Nov 30, 2019

@fregante OK, thank you. I will experiment on this in the coming days to see if I can do a PR.

@mleguen
Copy link
Author

mleguen commented Dec 3, 2019

PR submitted

@luckydrq
Copy link

I commented at the PR(#18 (comment)) and make a new PR(#20) to solve this. In a word, import foo = require('foo') need module foo have exports.default exported, or it would not work.

@papb
Copy link

papb commented Nov 25, 2020

In a word, import foo = require('foo') need module foo have exports.default exported, or it would not work.

This is not true. For import foo = require('foo') to work, the module needs to have export = foo in the entrypoint.

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

No branches or pull requests

5 participants