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

Unexpected error when importing export = with function declaration #36026

Closed
OliverJAsh opened this issue Jan 6, 2020 · 5 comments
Closed

Unexpected error when importing export = with function declaration #36026

OliverJAsh opened this issue Jan 6, 2020 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Jan 6, 2020

TypeScript Version: 3.7.3

Search Terms: import export equals = declaration types typings module function declaration expression

Code

// lib.d.ts
declare function md5(value: string, key?: string, raw?: boolean): string;

export = md5;
// Unexpected error:
// This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.
import * as lib from "./lib";

(Note: I have intentionally disabled allowSyntheticDefaultImports + esModuleInterop.)

Potential/known workarounds

If we use a function expression instead, the problem goes away:

// lib.d.ts
declare const md5: (value: string, key?: string, raw?: boolean) => string;

export = md5;

The problem also goes away if we add a namespace:

// lib.d.ts
declare function md5(value: string, key?: string, raw?: boolean): string;
declare namespace md5 {}

export = md5;
@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 6, 2020
@RyanCavanaugh
Copy link
Member

This is the intended behavior because import * as x can never produce a callable x per the ES spec. The other forms are allowed workarounds if you really intend to write some broken code 😉

@OliverJAsh
Copy link
Contributor Author

OliverJAsh commented Jan 7, 2020

Ah, thanks for clarifying that.

Given this is a valid CommonJS module definition, I'm wondering how we can import this into our TS project.

  • We can't use import md5 = require('blueimp-md5') because we're targeting ES Modules
  • We can't use import * as md5 from 'blueimp-md5' because ES Modules do not allow namespaces to be callable
  • We can't use import md5 from 'blueimp-md5' because we haven't enabled allowSyntheticDefaultImports, nor do we want to (because it can result in runtime exceptions and we want to adhere to the ES Modules spec)

Is this case, would you recommend using one of the workarounds I mentioned above, or is there something else we should consider?

Types for blueimp-md5: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/56c1ea26b59ed0e4634b1ba27096ab3b90371875/types/blueimp-md5/index.d.ts

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@ishowta
Copy link

ishowta commented May 17, 2022

Why doesn't the error occur when namespace is declared? Even in that case, I think the same runtime error occur when the function is called.

@RyanCavanaugh
Copy link
Member

Some people used downlevelers that made this incidentally work and took a hard dependency on that fact, so the namespace declaration is allowed as a workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants