FG
๐Ÿ—„๏ธ Databases

"import Redis" is flagged as invalid constructor function type in ESM TypeScript project

Freshabout 22 hours ago
Mar 14, 20260 views
Confidence Score60%
60%

Problem

I had to do this to get started [code block] Same issue as: [code block] They prints: `Type 'typeof import("node_modules/.pnpm/ioredis@5.2.3/node_modules/ioredis/built/index")' is not a constructor function type.` My project settings are: | package | version | | --------------- | ---------------| | Module Type | ESM | | Node.js | 16.13.2 | | pnpm | 7.9.5 | | ioredis | 5.2.3 | | TypeScript | 4.8.2 | | @types/ioredis | not installed | TSConfig: [code block]

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Invalid Constructor Function Type for ioredis in ESM TypeScript

Medium Risk

The error occurs because the ioredis library is being imported in a way that TypeScript does not recognize as a valid constructor function in an ESM environment. This is likely due to the way the library exports its modules, which can lead to issues when using TypeScript's strict type checking.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Install @types/ioredis

    Install the type definitions for ioredis to ensure TypeScript can properly recognize the types used in the library.

    bash
    pnpm add -D @types/ioredis
  2. 2

    Update Import Statement

    Change the import statement for ioredis to use a default import instead of a named import. This aligns with how the library exports its functionality.

    typescript
    import Redis from 'ioredis';
  3. 3

    Check TypeScript Configuration

    Ensure that your tsconfig.json has the correct settings for module resolution. Verify that 'module' is set to 'ESNext' or 'ES6' and 'esModuleInterop' is enabled.

    json
    {
      "compilerOptions": {
        "module": "ESNext",
        "esModuleInterop": true
      }
    }
  4. 4

    Rebuild the Project

    After making the changes, rebuild your TypeScript project to ensure that all changes are applied and the types are correctly recognized.

    bash
    pnpm run build

Validation

To confirm the fix worked, run your TypeScript project and check if the error regarding 'invalid constructor function type' is resolved. You should be able to instantiate Redis without any TypeScript errors.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

redisiorediscacheenhancement