question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

I had to do this to get started

import Redis from 'ioredis'

import type { Species } from '#lib/pokemon/Species.js'

export enum RedisKey {
  GetPokemon = `getPokemon`
}

export type RedisQuery<K extends RedisKey> = K extends `getPokemon` ? Species : null

export class RedisClient extends Redis {
  public async insert<K extends RedisKey>(
    key: K,
    query: RedisQuery<K>,
    data: unknown
  ): Promise<`OK`> {
    return super.set(`${key}:${query}`, JSON.stringify(data))
  }
}

Same issue as:

import { default as Redis } from 'ioredis'

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:

{
  "extends": "@sapphire/ts-config/extra-strict",
  "compilerOptions": {
    "lib": ["ESNext"],
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "Node16",
    "emitDecoratorMetadata": false
  }
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
GingerAdoniscommented, Oct 4, 2022

import { default as Redis } from "ioredis" is the solution documented on README.md but I agree the syntax may seem odd.

The problem is that despite using that import statement one still has to use Redis.default as a constructor to make it properly work with Typescript.

Example:

import { default as Redis } from 'ioredis';
const redis = new Redis.default();

The named Redis export is only exported as a type. Which is kind of strange as well.

5reactions
Sayakiecommented, Aug 30, 2022

Resolved as do:

import { default as Redis } from 'ioredis'

export class RedisClient extends Redis.default {
  // ... whatever
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript error: "This expression is not constructable" #1624
In TypeScript setting moduleResolution to nodenext throws This expression is not constructable. with this simple snippet: import Redis from " ...
Read more >
How To Use Modules in TypeScript | DigitalOcean
Modules are a way to organize your code into smaller, more manageable pieces, allowing programs to import code from different parts of the ......
Read more >
Typescript type error while using Redis type - node.js
Based on Node Redis documentation, it seems that the get method returns ... Not Found "); // go To ⏭️ function or middleware...
Read more >
error ts2351: this expression is not constructable. - You.com
Type 'typeof import("/Users/jurajzovinec/Documents/MY CLOUDTALK PROJECTS/campaigns/node_modules/ajv/dist/ajv")' has no construct signatures. validator function.
Read more >
ioredis | Yarn - Package Manager
A robust, performance-focused and full-featured Redis client for Node.js. redis, cluster, sentinel, pipelining. readme.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found