typescript module_error ai_generated true

TS2433: A namespace-style import cannot be called or constructed, and will cause a failure at runtime

ID: typescript/ts-namespace-merge-error

Also available as: JSON · Markdown
83%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

Namespace imports (* as X) cannot be called as functions. Use default or named imports instead.

generic

Workarounds

  1. 90% success Use default import or named imports instead of namespace import
    // Instead of: import * as lib from 'lib'; lib();
    // Use:
    import lib from 'lib'; lib();
    // Or:
    import { specificFn } from 'lib'; specificFn();

    Sources: https://www.typescriptlang.org/docs/handbook/modules/reference.html

  2. 85% success Enable esModuleInterop for CommonJS module compatibility
    // tsconfig.json:
    {
      "compilerOptions": {
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true
      }
    }

    Sources: https://www.typescriptlang.org/tsconfig#esModuleInterop

Dead Ends

Common approaches that don't work:

  1. Use allowSyntheticDefaultImports without understanding the module type 60% fail

    Only helps with CJS modules that have a default export; doesn't fix true namespace-only modules

Error Chain

Leads to:
Preceded by:
Frequently confused with: