fix: more typedefs improvements#366
Conversation
|
Thanks! I tested commit 519fd3b locally using this reproduction repo: Here’s the matrix I tried:
With this PR applied, both
(I'm not sure how common this configuration is in practice.) In my local testing, commenting out the |
|
@kyota-fujikura Thank you very much for the feedback. And you're right. Removing After cloning https://github.com/kyota-fujikura/iconv-lite-type-error-reproduction, I also added a import { encode, decode } from "iconv-lite";
const input = "こんにちは";
const encoding = "shift_jis";
const encoded = encode(input, encoding);
const decoded = decode(encoded, encoding);
console.log({ encoded, decoded });and scritps for running the files directly with NodeJS: "check-default-import-ts": "node src/default-import.ts",
"check-named-import-ts": "node src/named-import.ts",
"check-namespace-import-ts": "node src/namespace-import.ts"They all worked as expected. |
Pull Request Test Coverage Report for Build 20557192437Details
💛 - Coveralls |
|
For me it's good to, when the pr ? |
There was a problem hiding this comment.
Pull request overview
This pull request refactors the CommonJS export pattern in iconv-lite to improve TypeScript typedef compatibility. The changes align with the cjs-module-lexer compatibility patterns by switching from iconv.x = ... to exports.x = ... throughout the codebase, and updating the TypeScript definitions to use namespace-level exports with export = syntax.
Key Changes
- Replaced all
iconv.propertyassignments withexports.propertythroughoutlib/index.js - Restructured TypeScript definitions in
lib/index.d.tsfrom a nested object interface to namespace-level exports - Added
esModuleInterop: truetotsconfig.jsonfor better ES module/CommonJS interoperability
Reviewed changes
Copilot reviewed 1 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tsconfig.json | Added esModuleInterop: true compiler option for improved ES module compatibility |
| lib/index.js | Refactored all property assignments and method definitions from iconv.x pattern to exports.x pattern for proper CommonJS exports |
| lib/index.d.ts | Restructured type definitions from nested object structure to namespace-level exports matching the new JavaScript export style |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lib/index.js
Outdated
| @@ -102,7 +102,7 @@ iconv.getCodec = function getCodec (encoding) { | |||
| // | |||
| codec = new codecDef(codecOptions, iconv) | |||
There was a problem hiding this comment.
The variable iconv is used here but should be exports to match the refactoring throughout the rest of the file. Line 7 defines var iconv = module.exports, but since all other references have been changed to use exports directly, this reference should also be updated to exports for consistency.
| codec = new codecDef(codecOptions, iconv) | |
| codec = new codecDef(codecOptions, exports) |
There was a problem hiding this comment.
it should be module.exports or icons, imo
lib/index.js
Outdated
| } | ||
|
|
||
| iconv.encodingExists = function encodingExists (enc) { | ||
| exports.encodingExists = function encodingExists (enc) { |
There was a problem hiding this comment.
there's something that doesn't add up, and it's that technically with iconv.[nameOfObject] that's already being done, I can't understand why it doesn't work for TypeScript with the old way, but it does work with this new way, I feel like it's just a bug in attw that can't detect that rule properly
There was a problem hiding this comment.
It has to do with how TypeScript transpiles the userland code.
Also, since exports. is the right way to export modules (for use in ESM), it is an okay thing to do.
We want to verify how it behaves without that option, since it is disabled by default. Additionally, it’s not an option used in DefinitelyTyped for testing, so let’s leave it disabled.
ljharb
left a comment
There was a problem hiding this comment.
I prefer using module.exports over exports, since that helps prevent cycles, but otherwise the JS changes look correct to me.
lib/index.js
Outdated
| @@ -102,7 +102,7 @@ iconv.getCodec = function getCodec (encoding) { | |||
| // | |||
| codec = new codecDef(codecOptions, iconv) | |||
There was a problem hiding this comment.
it should be module.exports or icons, imo
bjohansebas
left a comment
There was a problem hiding this comment.
LGTM. I’ll leave it open for a bit longer so more people can take a look, and I’ll try to release it in January
Like in #330 (comment), the changes made in https://github.com/plbstl/iconv-lite/tree/more-typedefs-improvements resolves the issue.
Changing only the typings will not fully resolve the issue, as the JavaScript code will behave differently at runtime. The tests still pass, and were (technically) not modified.
You can see in https://github.com/nodejs/cjs-module-lexer's README, that
module.exports,module.exports.xandexports.xare shown. That's the right way to export CommonJS modules.cc @bjohansebas @prigaux @takayukioda @kyota-fujikura @Nirator78 . Can you review and verify that these changes work in the different environments and configs?
Closes #363