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

Deno doesn't support "allowSyntheticDefaultImports" compiler option which means a lot of npm dependencies that ship types won't typecheck properly #17058

Closed
itsdouges opened this issue Dec 15, 2022 · 3 comments

Comments

@itsdouges
Copy link

Hello! There's quite a few npm dependences that have typedefs for default exports that look like, well, a default export! Currently in Deno it results in the following error:

This expression is not callable.

As more folk start using Deno they're going to run into this problem, and it'll be a friction point that ideally shouldn't exist.

Other related issues:

Can Deno support the allowSyntheticDefaultImports compiler option, or do something to transparently make this work?

@dsherret
Copy link
Member

dsherret commented Dec 15, 2022

It's expected. We initially experimented with something to make this work, but now for node compat in Deno (which is always ESM) we decided to align with and follow the same rules that Node does in an ES module importing CJS. As I mentioned in the linked issues, those packages runtime code does module.exports = ..., but then define that in their types as export default, when it should be export =.

The bottom line is those packages need to fix their incorrect type declarations anyway if they want it to work when being imported by a Node ES module and then it will also work in Deno. I'd recommend if you come across a package like that to submit a pull request to their repo to correct their declarations and make it work correctly in both Node and Deno. In the meantime, you can write code like the following to "fix" the package's incorrect types:

import sizeofImport from "npm:[email protected]";

const sizeof = (sizeofImport as any as typeof sizeofImport.default);

console.log(sizeof({}));

In TypeScript, the allowSyntheticDefaultImports works because it's not using ES modules under the hood and instead transpiling to require with some injected code.

@itsdouges
Copy link
Author

Fair enough thanks for the context, I'll do that 😄. Might be worth putting something about this in the npm FAQ

@dsherret
Copy link
Member

Opened denoland/docs#95

@dsherret dsherret closed this as not planned Won't fix, can't repro, duplicate, stale Dec 16, 2022
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

2 participants