-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
import { default as ... } ...
doesn't work as of 4.8.3
#50690
Comments
TL;DR: the library’s types are wrong. @flatten-js/core is a CommonJS module (it ships some ESM files, but these are not exposed through any standard package.json fields). This means when you do a default import of it (or a
The way you represent this in a declare function box(): any;
declare const Flatten: {
box: typeof box;
}
export { box };
export { Flatten as default }; But instead, as you noted, the Now, you may object, saying that obviously we should just resolve to the // index.js
Object.defineProperty(exports, '__esModule', { value: true });
module.exports.foo = "foo";
module.exports.default = "default";
// index.d.ts
export declare const foo: "foo";
declare const _default: "default";
export default _default; What should we say the type of import lib from "my-wacky-lib"; By your hypothetical objection, it would look like The author of @flatten-js/core was clever to make The fact that |
@weswigham, please double check my explanation 😅 |
Chiming in here - while I understand that the old behavior working correctly may have been unintentional, this change in 4.8.3 led to a bunch of breaking changes in my code, which doesn't seem right for a point release. The For example, see the ioredis ESM import snippet (which was correct as of 4.8.2 but no longer works): https://github.com/luin/ioredis#basic-usage This worked in 4.8.2: import { default as Redis } from "ioredis";
const redisConn: Redis = new Redis(); but is broken in 4.8.3, needing to be rewritten as: import { default as Redis } from "ioredis";
const redisConn: Redis.Redis = new Redis.default(); to compile. I had to make a bunch of similar changes to fastify plugin imports as well. It looks like ajv is affected as well (ajv-validator/ajv#2047), and likely many other packages. This seems like a pretty major non-backwards-compatible change for a point release. |
Thank you for such a thorough explanation, @andrewbranch! I'll report this in Flatten instead. |
@dkulchenko yeah, I understand. We didn’t realize that the bug was being used as a workaround for broken typings. Knowing what I know now, I probably would have held the fix from 4.8.3, but the proverbial toothpaste is out of the tube now. One correction, though—the typings that are broken were always wrong; they didn’t just need to be updated for |
Haven't heard this one before! It just sounds so... prosaic, compared to more colorful expressions like the cat being out of the bag, or the genie being out of the bottle, or closing the barn door after the horse escaped... |
The workaround becomes even stranger than before 😅 Update: the workaround seems only works for type checking, but the compiled result would failed to be used in other tools like Vite |
I'm stuck on a similar situation. I am writing ESM code with TypeScript 4.8.3 and // node_modules/sql.js/dist/sql-wasm-debug.js, lines 7061-7063
function initSqlJs() { /* ... */ };
module.exports = initSqlJs;
module.exports.default = initSqlJs; and whose type definitions look like: // node_modules/@types/sql.js/index.d.ts
export interface InitSqlJsStatic extends Function {
(config?: SqlJsConfig): Promise<SqlJsStatic>;
readonly default: this;
}
declare global {
let SqlJs: InitSqlJsStatic;
}
export default SqlJs; ...and when I attempt to call import initSqlJs from 'sql.js';
// error TS2349: This expression is not callable.
// Type 'typeof import("/path/to/my/project/node_modules/@types/sql.js/index")' has no call signatures.
initSqlJs(); ...even though the code runs fine in Node.js. If the fault lies with the type definitions for |
It seems like they’re trying to do a UMD module interface InitSqlJsStatic extends Function {
(config?: SqlJsConfig): Promise<SqlJsStatic>;
readonly default: this;
}
declare const SqlJs: InitSqlJsStatic;
declare namespace SqlJs {
export { InitSqlJsStatic };
}
export = SqlJs;
export as namespace SqlJs; |
Rewrite the export statements so that the package can be used correctly in projects using `module: "Node16"` as well as browser scripts. Previously, the type definitions for sql.js incorrectly modeled how the library actually behaves. The typings used `export default` even though sql.js is actually a UMD library. This works fine in projects with `module: "ESNext"`, but caused errors when using `module: "Node16"` due to a recent bugfix in TypeScript 4.8.3+. For details, see microsoft/TypeScript#50690 . Also, the type definitions previously used 'SqlJs' as the global namespace for browser script contexts. However, the library actually uses `initSqlJs` when loaded with the <script> tag in browsers. This caused compiler errors when using `/// <reference types="sql.js" />`. These issues have been fixed by using `export =`, `import x = require()` and `export as namespace` statements with the correct identifier.
…nvironments by @pastelmind Rewrite the export statements so that the package can be used correctly in projects using `module: "Node16"` as well as browser scripts. Previously, the type definitions for sql.js incorrectly modeled how the library actually behaves. The typings used `export default` even though sql.js is actually a UMD library. This works fine in projects with `module: "ESNext"`, but caused errors when using `module: "Node16"` due to a recent bugfix in TypeScript 4.8.3+. For details, see microsoft/TypeScript#50690 . Also, the type definitions previously used 'SqlJs' as the global namespace for browser script contexts. However, the library actually uses `initSqlJs` when loaded with the <script> tag in browsers. This caused compiler errors when using `/// <reference types="sql.js" />`. These issues have been fixed by using `export =`, `import x = require()` and `export as namespace` statements with the correct identifier. Co-authored-by: Yehyoung Kang <[email protected]>
Hi, I'm having the same issue using |
@Mihai-github can you give us some more details on what you’re doing (including tsconfig settings) and what you’re seeing? |
@adamburgess Sure, thanks.
My issue is probably the same with others with When I'm trying to import the component in other files using the name given with this current version of typescript I cannot make it work because I get an error telling |
What import/re-export is giving you the problem? |
How do I fix this issue for a ESM-only javascript package the types for which are provided by |
@GabenGar a package.json with |
This works for me... 🤷♂️ import RedisModule from "ioredis";
const Redis = RedisModule.default; |
Hi there, this issue is heavily effecting my projects. I am targeting pure esm packages with There is a lot of packages in my repo with is exporting both esm files with I can only managed to fix types issues with them by adding
With just one hour, I am already sending severl prs to upsteam. including:
The examples above are popular packages,(e.g.: mitt is getting Weekly Downloads at 1,960,326). If the whole ecosystem has a huge part that needs to change according to this, I am thinking that the change might be too breaking for a minor version. @andrewbranch Could you give some feedback with this? |
I read again https://nodejs.org/dist/latest-v18.x/docs/api/packages.html#exports, it seems that my solution adding |
Bug Report
🔎 Search Terms
export default
,import default as
, namespace, declaration.🕗 Version & Regression Information
This changed between versions 4.8.2 and 4.8.3.
⏯ Playground Link
I can't provide a link because both Bug Workbench and CodeSandbox still don't have 4.8.3.
💻 Code
Complete typings.
🙁 Actual behavior
I get the following error:
When looking at
Flatten
, it still has adefault
within, so I need to accessPolygon
withFlatten.default.Polygon
.🙂 Expected behavior
It gets unwrapped correctly, like in 4.8.2 and before. Right now I'm having to use the following workaround:
The text was updated successfully, but these errors were encountered: