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

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

Closed
Sayakie opened this issue Aug 30, 2022 · 14 comments

Comments

@Sayakie
Copy link

Sayakie commented Aug 30, 2022

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
  }
}
@Sayakie
Copy link
Author

Sayakie commented Aug 30, 2022

Additional info for more context, new Redis() throws similar error: "This expression is not constructable. Type 'typeof import("node_modules/.pnpm/ioredis@5.2.3/node_modules/ioredis/built/index")' has no construct signatures."

Append "new Redis()" to initialize ioredis class, but it throws "This expression is not constructable." in fly-in-compile-time on Visual Studio Code

@Sayakie
Copy link
Author

Sayakie commented Aug 30, 2022

Resolved as do:

import { default as Redis } from 'ioredis'

export class RedisClient extends Redis.default {
  // ... whatever
}

@Sayakie Sayakie closed this as completed Aug 30, 2022
@GingerAdonis
Copy link

I'd say this issue should be re-opened as the workaround is kind of odd.

@Sayakie
Copy link
Author

Sayakie commented Sep 30, 2022

I'd say this issue should be re-opened as the workaround is kind of odd.

I assume would be better open a new ticket.

@GingerAdonis
Copy link

GingerAdonis commented Sep 30, 2022

I'd say this issue should be re-opened as the workaround is kind of odd.

I assume would be better open a new ticket.

I disagree, as it would be a duplicate of this unresolved issue. But I'll do that if you don't want to open the issue.

@Sayakie Sayakie reopened this Sep 30, 2022
@Sayakie
Copy link
Author

Sayakie commented Sep 30, 2022

@luin I re-open this issue because the behavior is odd and still would be problem.

Tracking of:

@luin
Copy link
Collaborator

luin commented Sep 30, 2022

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

@GingerAdonis
Copy link

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.

@LinusU
Copy link

LinusU commented Nov 8, 2022

I can confirm that the normal import doesn't work when using moduleResolution node16 or nodenext. We are currently using the following workaround:

import RedisModule from 'ioredis'

// FIXME: https://github.com/luin/ioredis/issues/1642
const Redis = RedisModule.default

@huangyingjie
Copy link

I can confirm that the normal import doesn't work when using moduleResolution node16 or nodenext. We are currently using the following workaround:

import RedisModule from 'ioredis'

// FIXME: https://github.com/luin/ioredis/issues/1642
const Redis = RedisModule.default

I also use node16, and trapped by this @luin

2 similar comments
@huangyingjie
Copy link

I can confirm that the normal import doesn't work when using moduleResolution node16 or nodenext. We are currently using the following workaround:

import RedisModule from 'ioredis'

// FIXME: https://github.com/luin/ioredis/issues/1642
const Redis = RedisModule.default

I also use node16, and trapped by this @luin

@huangyingjie
Copy link

I can confirm that the normal import doesn't work when using moduleResolution node16 or nodenext. We are currently using the following workaround:

import RedisModule from 'ioredis'

// FIXME: https://github.com/luin/ioredis/issues/1642
const Redis = RedisModule.default

I also use node16, and trapped by this @luin

@GingerAdonis
Copy link

GingerAdonis commented Feb 13, 2023

Looks like this issue has been fixed in v5.2.5.

Issue can be closed.

@Sayakie
Copy link
Author

Sayakie commented Feb 13, 2023

Fixed in #1695

@Sayakie Sayakie closed this as completed Feb 13, 2023
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

5 participants