ESM Import Failing - v6.0.0
dscalzi opened this issue · comments
minimatch - 6.0.0
typescript - 4.9.4
node - 16.17.0
Error:
src/structure/spec_model/module/Module.struct.ts:218:59 - error TS2349: This expression is not callable.
Type 'typeof import("F:/Project/node_modules/minimatch/dist/cjs/index")' has no call signatures.
218 return this.untrackedFilePatterns.find(pattern => minimatch(pathRelativeToContainer, pattern))
Usage:
import minimatch from 'minimatch'
protected isFileUntracked(pathRelativeToContainer: string): string | undefined {
return this.untrackedFilePatterns.find(pattern => minimatch(pathRelativeToContainer, pattern))
}
tsconfig
{
"compilerOptions": {
"target": "ES2021",
"module": "Node16",
"lib": ["ES2021"],
"sourceMap": true,
"outDir": "dist",
"moduleResolution": "Node16",
"esModuleInterop": true
}
}
What version of typescript are you using? This works for me:
$ npm ls
foo@ /Users/isaacs/dev/isaacs/foo
├── minimatch@6.0.0
└── typescript@4.9.4
import minimatch from 'minimatch'
export class Test {
untrackedFilePatterns: string[] = ['a', 'b', 'c']
protected isFileUntracked(
pathRelativeToContainer: string
): string | undefined {
return this.untrackedFilePatterns.find(pattern =>
minimatch(pathRelativeToContainer, pattern)
)
}
}
Oh, you said you're using 4.9.4. Then I wonder what's going on there...?
Aha, I was able to reproduce it. This is weird.
In the short term, you can import { minimatch } from 'minimatch'
and it'll work fine. It seems to be some confusion with the default export also being exported as a named export, and specific combination of tsconfig options here. I'll update the readme to make import { minimatch }
the suggested inclusion mechanism.
That worked 👍
It's really weird, like, I see this in the d.ts:
export declare const minimatch: {
// v-- THIS IS A CALL SIGNATURE, WAT!?
(p: string, pattern: string, options?: MinimatchOptions): boolean;
sep: string;
GLOBSTAR: typeof GLOBSTAR;
filter: (pattern: string, options?: MinimatchOptions) => (p: string) => boolean;
defaults: (def: MinimatchOptions) => typeof minimatch;
braceExpand: (pattern: string, options?: MinimatchOptions) => string[];
makeRe: (pattern: string, options?: MinimatchOptions) => false | MMRegExp;
match: (list: string[], pattern: string, options?: MinimatchOptions) => string[];
Minimatch: typeof Minimatch;
};
// v-- IT IS THE DEFAULT EXPORT! THE THING WITH THE CALL SIGNATURE!
export default minimatch;
but then:
This expression is not callable.
Type 'typeof import("/Users/isaacs/dev/isaacs/foo/node_modules/minimatch/dist/cjs/index")' has no call signatures. (tsserver 2349)
Possibly a typescript bug, I know it took them a significant effort to support ESM in the first place.
Aha, if you use "moduleResolution": "node"
instead of "node16"
then it works. Not sure what the difference is there.
That disables ESM and uses commonjs https://www.typescriptlang.org/tsconfig#node16nodenext-nightly-builds
Ok, this gets even stranger. After experimenting with it a bit and trying to work out a minimal test case, now I can't reproduce the error, even starting from the exact same setup where I was reproducing it before. This definitely feels like a strangely non-deterministic TS bug. I'm going to update the readme and call it fixed 😂
Works for me, I prefer names imports anyway haha
This is the TS issue: microsoft/TypeScript#50466
"Proper" (ugly) fix incoming.
Just to follow up here, I tried using const minimatch = require('minimatch');
and that failed as well. I had to switch to const { minimatch } = require('minimatch');
for it to work again.
@redonkulus What version of minimatch? Should be fixed on latest.
I tried this morning on the latest version from npm.
@redonkulus Ah, indeed. Updated "main"
but not exports['.'].require
, sorry about that.
Fixed on a4fa1f8, published 6.1.6
import { minimatch } from "minimatch";
It works for me.