"import Redis" is flagged as invalid constructor function type in ESM TypeScript project
See original GitHub issueI 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:
- Created a year ago
- Reactions:3
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
The named Redis export is only exported as a type. Which is kind of strange as well.
Resolved as do: