Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
karlhorky opened this issue Mar 9, 2023 · 16 comments
Closed

Comments

@karlhorky
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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... 🤔

"module": "none",
"moduleResolution": "node",
"outDir": "./dist",
"sourceMap": true,
"strict": true,
"target": "es6"

@karlhorky
Copy link
Contributor Author

karlhorky commented Mar 10, 2023

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

@sebbo2002
Copy link
Owner

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
Copy link
Contributor Author

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 added a commit to upleveled/ical-move-events that referenced this issue Mar 19, 2023
@karlhorky
Copy link
Contributor Author

karlhorky commented Mar 24, 2023

@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

@karlhorky karlhorky reopened this Mar 24, 2023
@sebbo2002
Copy link
Owner

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
Copy link
Contributor Author

karlhorky commented Mar 24, 2023

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

@sebbo2002
Copy link
Owner

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
Copy link
Contributor Author

karlhorky commented Mar 24, 2023

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
Copy link
Owner

Fixed with 🎉 4.0.0

@karlhorky
Copy link
Contributor Author

karlhorky commented Mar 30, 2023

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 #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
Copy link
Contributor Author

karlhorky commented Mar 30, 2023

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
Copy link
Owner

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
Copy link
Contributor Author

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
Copy link

marc-wittwer commented Nov 3, 2024

@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: microsoft/TypeScript#50175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants