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

TypeScript typings are incompatible with Node.js environment #517

Closed
octogonz opened this issue Sep 3, 2023 · 4 comments
Closed

TypeScript typings are incompatible with Node.js environment #517

octogonz opened this issue Sep 3, 2023 · 4 comments

Comments

@octogonz
Copy link

octogonz commented Sep 3, 2023

ical-generator/ical-generator/dist/calendar.d.ts

    /**
     * Generates a blob to use for downloads or to generate a download URL.
     * Only supported in browsers supporting the Blob API.
     *
     * Unfortunately, because node.js has no Blob implementation (they have Buffer
     * instead), this method is currently untested. Sorry Dave…
     *
     * @since 1.9.0
     */
    toBlob(): Blob;

When this .d.ts file is imported into a TypeScript project, the project fails to compile with this error message:

Error: node_modules\.pnpm\ical-generator@3.5.0-develop.1_6cpkm3xmcfrw36duhz3wk24ofa\node_modules
\ical-generator\dist\calendar.d.ts:402:15 - (TS2304) Cannot find name 'Blob'.

The reason is that the Blob API is not available in Node.js and thus is not defined by @types/node, as noted in the comments.

Workaround

The workaround is to set "skipLibCheck": true in tsconfig.json, which disables type checking for imported libraries. Some people recommend that practice, because less checking means tsc runs faster.

However it is obviously bad for professional projects: If the compiler reports an error for an imported .d.ts file, that means the compiler's analysis of those APIs is incorrect or incomplete. In other words, actual mistakes in your program may not get caught by the compiler, because it failed to compile your library typings correctly.

Therefore professional projects should ensure there are no such errors, which can only be accomplished by setting "skipLibCheck": false. Libraries should make sure that they can be successfully compiled in this mode.

Recommended fix

A simple solution would be to declare the API as toBlob(): any. A more sophisticated solution would be to declare a global type Blob = any which can get restricted by the real Blob API declaration when/if it is imported.

@sebbo2002
Copy link
Owner

I'm currently on vacation until mid-September and won't get to a fix before then. toBlob(): any seems a bit very hacky to me, but if someone could prepare a PR for the second proposal, I would look at it promptly.

@sebbo2002
Copy link
Owner

I can't think of a solution off the top of my head that wouldn't be a breaking change for the toBlob() method, so I wouldn't pursue this issue further and instead refer to #478 which also fixes this issue. If there is a solution that would not be a breaking change, I would be very grateful for specific advice.

@sebbo2002
Copy link
Owner

I just pushed a preview of ical-generator v6 to develop, which should fix this issue. I would be happy if you could test the new version and give me some feedback. You can install the version with npm i ical-generator@next. All changes in this release can be found here.

@octogonz
Copy link
Author

@sebbo2002 Yes, it works. Thank you! 👍

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

No branches or pull requests

2 participants