angus-c/just

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

Open

#421 opened on Feb 16, 2022

View on GitHub
 (5 comments) (2 reactions) (0 assignees)JavaScript (5,494 stars) (228 forks)batch import
help wanted

Description

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):

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

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

Contributor guide

Just packages don't work with TS when "esModuleInterop": false (default) · angus-c/just#421 | Good First Issue