Typescipt and ESM yields "Ajv This expression is not constructable" #2132
Closed
Description
I have a TS project in ESM mode (type:module) and getting VSCode errors using certain modules such as Ajv..
Given this in my tsconfig.json:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "nodenext",
"esModuleInterop": true
// I had a lot more stuff but the top two lines are the ones causing issue (Node16 is same)
}
}
package.json
{
"name": "test",
"type": "module",
"dependencies": {
"ajv": "^8.11.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.3",
"@types/node": "^18.11.0",
"typescript": "^4.8.3"
}
}
and finally code:
import Ajv from "ajv"
export const getClient = ():Ajv => {
return new Ajv()
}
Note that this code compiles correctly.
If I do:
import Ajv from "ajv"
export const getClient = ():Ajv.default => {
return new Ajv.default()
}
Then error goes away but it does not run.
I think this is something to do with CJS modules needing to add something for esm export but not sure exactly what.
Here's a sandbox