sebbo2002 / ical-generator

ical-generator is a small piece of code which generates ical calendar files
MIT License
748 stars 162 forks source link

"This expression is not callable" with module: "node16" in tsconfig.json #469

Closed karlhorky closed 1 year ago

karlhorky commented 1 year ago

Hi @sebbo2002 πŸ‘‹ Thanks again for ical-generator, very useful!

The new module resolution option node16 does not work with ical-generator unfortunately, failing with the This expression is not callable error:

This expression is not callable.
  Type 'typeof import("stackblitz:/node_modules/ical-generator/dist/index")' has no call signatures.

StackBlitz demo: https://stackblitz.com/edit/node-mmyw9w?file=tsconfig.json,package.json&file=tsconfig.json,index.ts

Screenshot 2023-03-09 at 22 07 20

The problem is the export default in the published index.d.ts, as far as I understand, since ical-generator uses a CommonJS module.exports = export, this should actually instead be export =.


You can also see this on the tool Are The Types Wrong from @andrewbranch:

Screenshot 2023-03-09 at 22 09 07
sebbo2002 commented 1 year ago

Thanks for this great bug report. Unfortunately, I don't see at first glance why the type definitions are being generated incorrectly and since I have a visitor this weekend, I probably won't be able to deal with it extensively until next week. I hope that's ok, otherwise pull requests are always appreciated of course. Thank you.

karlhorky commented 1 year ago

Also not 100% sure what tsconfig.json options cause this, but if I would have to guess, I would look at this compilerOptions.target: 'es6' - seems like an outdated module format. Also module: 'none' is interesting... πŸ€”

https://github.com/sebbo2002/ical-generator/blob/f105df90729bfa46e477922303b2ce1222a89aa4/tsconfig.json#L5-L10

karlhorky commented 1 year ago

Maybe the compilerOptions.target is actually fine and module: "commonjs" is needed, as per this Stack Overflow answer?

sebbo2002 commented 1 year ago

Do you want to try it with the current develop version (ical-generator@4.0.0-develop.1)?

BildschirmΒ­foto 2023-03-17 um 23 15 49
karlhorky commented 1 year ago

Forked the StackBlitz demo to a new one, seems like it's working πŸ™Œ

Thanks! Closing this.

Updated (working) StackBlitz demo: https://stackblitz.com/edit/node-5l65bv?file=tsconfig.json,package.json&file=tsconfig.json,index.ts

Screenshot 2023-03-19 at 19 21 43
karlhorky commented 1 year ago

@sebbo2002 reopening this - ical-generator@4.0.0-develop.2 is broken again:

StackBlitz Demo: https://stackblitz.com/edit/node-rrwxxe?file=package.json&file=index.ts

Screenshot 2023-03-24 at 09 53 50

Also check it out on Are The Types Wrong?

Screenshot 2023-03-24 at 09 50 09
sebbo2002 commented 1 year ago

Yes, I've seen when merged it in, but I don't understand the error yet and I haven't had time to look at it. The .d.ts file is the same, yes, but I only have one type definition file after running tsup. If I duplicate the file so that the filenames are different, everything is okay. But that can't possibly be the cause.

karlhorky commented 1 year ago

Haven't looked at your project configuration / publishing formats yet, but maybe you need *.d.mts files like this?

sebbo2002 commented 1 year ago

That's what I meant with duplicating the file. Okay. I will play around a bit more on the weekend, feel free to use the develop.1 for this time. I'll update the ticket here when I think I'm done with customizations πŸ˜…

karlhorky commented 1 year ago

Yeah the *.d.mts files seemed counterintuitive to me at first too, but Andrew Branch suggested them when I was doing some other PRs fixing types in libraries, so I think it's a good way to go.

sebbo2002 commented 1 year ago

Fixed with πŸŽ‰ 4.0.0

karlhorky commented 1 year ago

Great, thanks so much for this! πŸ™Œ

I can confirm that it's working - just upgraded to ical-generator@4.0.0 in our project ical-move-events and it works πŸŽ‰

I would have a question: how did you get the compiler to emit the dist/index.d.cts file?

Was it just this change to the tsconfig.json in https://github.com/sebbo2002/ical-generator/pull/462?

-        "module": "none",
-        "moduleResolution": "node",
+        "module": "nodenext",
+        "moduleResolution": "nodenext",

I'd like to learn how you did it to be able to recommend things for other packages too...

If you have any other changes that should be highlighted, happy to hear about those too!

karlhorky commented 1 year ago

Oh wait, maybe it's this manual cp that you did in package.json? πŸ€”

-    "build": "tsc",
+    "build": "tsup && cp ./dist/index.d.ts ./dist/index.d.cts",

I thought there would have been a way for tsc to create these files automatically 😯

sebbo2002 commented 1 year ago

Yep, it's the manual cp. Directly with tsc or tsup I did not find a way to create the *.d.cts file. Specifying the same file in package.json generates the "Masquerading as CJS" error message, symlinks don't work unfortunately. Whereby I am honestly unsure if "Masquerading as CJS" is really an issue. But this way it works in any case, even if it is a bit messy with the cp.

karlhorky commented 1 year ago

Yeah, any issue in Are The Types Wrong? is an issue, any "Masquerading as ..." error is going to cause the same type of errors when importing / requireing that I reported.

Thanks for the tip with cp! If I see anything else better (or find a way to do it in tsc), I'll try to report back here! Would love to be able to make this as zero-config and easy for lib authors / maintainers as possible!

marc-wittwer commented 1 week ago

@sebbo2002 It seems that this currently does not work for "node16" in tsconfig.json with Node v20.9.0 and v20.11.1

import ical from "ical-generator";

const cal = ical();

Error:
src/index.ts(13,15): error TS2349: This expression is not callable.
  Type 'typeof import("/project/workspace/node_modules/ical-generator/dist/index")' has no call signatures.

Reproduction example: https://codesandbox.io/p/devbox/simple-express-server-forked-vvg49x?file=%2Fpackage.json%3A14%2C26

//tsconfig.json
 "compilerOptions": {
        "target": "ES2022",
        "module": "Node16" 
        "moduleResolution": "Node16",

I looked into ways to fix it:

Option 1:

// Use the calendar item class directly:
import { ICalCalendar } from "ical-generator";
const calendar = new ICalCalendar();
console.log(calendar);

// This works without any problems

Option 2: Add a named export to the ical-generator package for ical.

// src/index.ts in ical-generator
-  function ical(data?: ICalCalendarData): ICalCalendar {
+ export function ical(data?: ICalCalendarData): ICalCalendar {
    return new ICalCalendar(data);
}

export default ical;

// usage:
import { ical } from "ical-generator";

It could be that it is related to this: https://github.com/microsoft/TypeScript/issues/50175