angus-c / angus-c/just

Just packages don't work with TS when "esModuleInterop": false (default)

Abierto
#421 5 comentarios 2 reacciones 0 asignados Ver en GitHub
help wanted
Lenguaje dominante
JavaScript
Estrellas
6.2k
Forks
210
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I have the following tsconfig.json:

```
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2020",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"paths": {
"src/*": ["src/*"]
}
}
}
```

Please note that it has `"allowSyntheticDefaultImports": true` and no `"esModuleInterop"` set, which means that it equals to false by default.

So with this setup, we usually import most of our packages with `* as something`, like so `import * as cookieParser from 'cookie-parser'`.

However with Just packages, like "just-pick", we can't import it at all, because both ways don't work for us (i.e. with `* as ` or without it).

Importing a package like so makes the imported package to be `undefined` (with no TS type-checking errors):

```ts
import pick from 'just-pick'

console.log(pick) // <-- undefined
```

Importing like so works on the runtime but the TS type-cheker would shows an error

```ts
import * as pick from 'just-pick'

pick(obj, []) // <-- This expression is not callable. Type 'typeof import(".../node_modules/just-pick/index")' has no call signatures ts(2349)

// Howeever this way it works
(pick as any)(obj, [])
```

So we had to add `"esModuleInterop": true` to our tsconfig.json to make it work, which isn't that bad option, just feel like it's something that should be reported. We have quite a big project with a lot of packages working well, but installing Just packages didn't work.

**PS:**

Node version: v14.18.3
NPM version: 7.24.2
Typescript version: ^4.2.3

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.