ts-transformer-keys icon indicating copy to clipboard operation
ts-transformer-keys copied to clipboard

Can't import keysTransformer in a normal way

Open kwyntes opened this issue 1 year ago • 2 comments

Not sure how to better express it than that, in the compiled JS file (transformer.js) in the NPM package, the transformer factory function is exported like this:

exports.default = transformer;

This means (for whatever weird and convoluted JS module export syntax reasons or whatever) that the module will now be imported as { default: [Function: transformer] }, meaning the code would have to look like this:

import keysTransformer from 'ts-transformer-keys/transform.js';

const theActualKeysTransformer = keysTransformer.default;

The export should go like this:

export default transformer

I suppose this is a TypeScript compilation target issue or something like that?

Perhaps the module option under compilationOptions in tsconfig.json should be set to es6?

kwyntes avatar Jun 05 '23 13:06 kwyntes

Main reason this is an issue is this way of importing the module is actually in conflict with the type definitions of it, meaning that TypeScript will not let you import the module by doing (await import(...)).default.default eventhough that is the correct way to successfully import it as a result of the weird exports.default = notation being used in the compiled JavaScript file.

kwyntes avatar Jun 05 '23 13:06 kwyntes

Yes, it is probably a good idea to include the ESM version of transformer.js into this package to make it a dual CommonJS/ES module package.

I will consider doing that. But I guess your issue can be simply solved by changing the TypeScript compilation configuration, such as enabling esModuleInterop.

kimamula avatar Jun 10 '23 08:06 kimamula