Open peterhirn opened 1 year ago
Hi.
First of all, there're no ./types.d.js
files in the repo, only ./types.d.ts
ones.
Second, regular TypeScript doesn't allow importing files with .ts
extension:
https://bobbyhadz.com/blog/typescript-import-path-cannot-end-with-ts-extension
Importing files with an additional .js
extension is necessary when using TypeScript + ECMAScript Modules in Node.js, see https://www.typescriptlang.org/docs/handbook/esm-node.html
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result, it will have to be rewritten to use the extension of the output of foo.ts - so bar.ts will instead have to import from ./foo.js.
ps. my app has about 30 direct dependencies and read-excel-file
is the only one which doesn't work with "moduleResolution": "nodenext"
.
I see. So looks like TypeScript ESM is incompatible with regular TypeScript. It’s weird that they haven’t implemented backwards compatibility though similsr to Node.js’es where they support importing non-type-module modules from type-module modules. Most likely there is backwards compatibility, just under some flag.
On Wed, 8 Feb 2023 at 10:40, peterhirn @.***> wrote:
Importing files with an additional (non-existent) .js extension is necessary when using TypeScript + ECMAScript Modules in Node.js, see https://www.typescriptlang.org/docs/handbook/esm-node.html
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result, it will have to be rewritten to use the extension of the output of foo.ts - so bar.ts will instead have to import from ./foo.js.
ps. my app has about 30 direct dependencies and read-excel-file is the only one which doesn't work with "moduleResolution": "nodenext".
— Reply to this email directly, view it on GitHub https://github.com/catamphetamine/read-excel-file/issues/111#issuecomment-1422165883, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADUP35P7EMZQU3IIXWOV5LWWNEXFANCNFSM6AAAAAAUUKRJ4Y . You are receiving this because you commented.Message ID: @.***>
I couldn't find any additional compatibility flag. It seems adding .js
to typescript imports always works, regardless of moduleResolution
config. But moduleResolution: nodenext
requires it.
When using TypeScript with native ECMAScript Module support (eg.
"moduleResolution": "nodenext"
) and"type": "module"
importing fromread-excel-file/node
produces the following error:I patched
index.d.ts
andnode/index.d.ts
and this seems to work, even for CJS imports and other settings (eg."moduleResolution": "node"
) :Not sure if this is a good patch or if it would break other users code. Feedback from anyone who's more familiar with TypeScript and native ECMAScript Module support would be appreciated.