-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Losing types when importing cjs module using default export with nodenext modules #48845
Comments
Is your file a CJS or ES module? (If you’re not 105% sure: what is its file extension and what is its nearest package.json’s |
Fastify plugins are definitely CJS, the project I'm importing those to is ESM (type: "module", module: "nodenext", moduleResolution: "nodenext") |
When using named import, TS successfully compiles, but node throws error:
It actually works when following example from error, still wonder if it's a typescript bug or not // plugin.js
/**
* These export configurations enable JS and TS developers
* to consume fastify-cookie in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const fastifyCookie = require('fastify-cookie')`
* - `const { fastifyCookie } = require('fastify-cookie')`
* - `import * as fastifyCookie from 'fastify-cookie'`
* - `import { fastifyCookie } from 'fastify-cookie'`
* - `import fastifyCookie from 'fastify-cookie'`
*/
fastifyCookie.fastifyCookie = fastifyCookie
fastifyCookie.default = fastifyCookie
module.exports = fastifyCookie // plugin.d.ts
declare const fastifyCookie: FastifyPluginCallback<NonNullable<FastifyCookieOptions>>;
export default fastifyCookie;
export { fastifyCookie }; |
I see #13626 is still open, so there's no way to specify type of default export |
#13626 is not related to this. Fastify just wrote wrong types. Unfortunately the only thing to do is open an issue, fork and fix, or |
Uh, I see that in this case, the only "easy" fix is to support named import for fastify-static and follow nodejs error recommendation
BTW, shouldn't TS compiler throw error when trying to make named import of CJS module like node does in runtime? |
* currently it shows error: This expression is not callable. Type 'typeof import("node_modules/vite-plugin-checker/lib/main")' has no call signatures.ts(2349) * see microsoft/TypeScript#48845
* make type definitons `"module": "nodenext"` compatible * see microsoft/TypeScript#48845 * fix test * fix default export type * make typing extra safe and small refactoring * restore brackets * add additional properties to named export and fix test suite * remove formatting * revert formatting * reuse type
…#140) * add named export to improve `"module": "nodenext"` compatibility * currently it shows error: This expression is not callable. Type 'typeof import("node_modules/vite-plugin-checker/lib/main")' has no call signatures.ts(2349) * see microsoft/TypeScript#48845 * switch to cjs module style * Revert "switch to cjs module style" This reverts commit ef7a774. * remove extra semicolon * fix patch
* make type definitons `"module": "nodenext"` compatible * see microsoft/TypeScript#48845 * fix test * fix default export type * make typing extra safe and small refactoring * restore brackets * add additional properties to named export and fix test suite * remove formatting * revert formatting * reuse type
* integrate cookie signer * add algorithm as plugin-option * check for matching error message * improve tests * remove unnecessary array conversion * simplify * fix comment * restructure SignerFactory * update typings * fix exports * add benchmarks * improve perf for multi secrets * rename SignerFactory to Signer * Merge branch 'master' into integrate-cookie-signer * export signerFactory * Update signer.js * add secure: 'auto' Option (#199) * add secure: auto Option * document deviation from cookie serialize * describe better * lowercase Lax * Bumped v7.3.0 Signed-off-by: Matteo Collina <[email protected]> * make type definitons `"module": "nodenext"` compatible (#184) * make type definitons `"module": "nodenext"` compatible * see microsoft/TypeScript#48845 * fix test * fix default export type * make typing extra safe and small refactoring * restore brackets * add additional properties to named export and fix test suite * remove formatting * revert formatting * reuse type * Bumped v7.3.1 Signed-off-by: Matteo Collina <[email protected]> * modify types * remove dummy * fix regex bottleneck found by sonarqube Signed-off-by: Matteo Collina <[email protected]> Co-authored-by: Matteo Collina <[email protected]> Co-authored-by: Volodymyr Zhdanov <[email protected]>
Bug Report
🔎 Search Terms
cjs typescript nodenext default export
🕗 Version & Regression Information
4.7.0-dev
While using nodenext with pure cjs modules, such as https://github.com/fastify/fastify-cookie or https://github.com/fastify/fastify-static , types are lost when using default export and cjs named export is prohibited
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
🙂 Expected behavior
Type should not be lost while import
Can be workaround by
But i'm not sure if that's expected or not
The text was updated successfully, but these errors were encountered: