Skip to content
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

TypeScript problem with native ECMAScript Module support #111

Open
peterhirn opened this issue Feb 7, 2023 · 4 comments
Open

TypeScript problem with native ECMAScript Module support #111

peterhirn opened this issue Feb 7, 2023 · 4 comments

Comments

@peterhirn
Copy link

When using TypeScript with native ECMAScript Module support (eg. "moduleResolution": "nodenext") and "type": "module" importing from read-excel-file/node produces the following error:

> tsc --build

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:13:8 - error TS2307: Cannot find module '../types.d' or its corresponding type declarations.

13 } from '../types.d';
          ~~~~~~~~~~~~

node_modules/.pnpm/read-excel-file@5.6.0/node_modules/read-excel-file/node/index.d.ts:21:8 - error TS2307: Cannot find module '../types.d' or its corresponding type declarations.

21 } from '../types.d';

I patched index.d.ts and node/index.d.ts and this seems to work, even for CJS imports and other settings (eg. "moduleResolution": "node") :

--- a/index.d.ts
+++ b/index.d.ts
@@ -4,7 +4,7 @@ import {
 	ParseWithoutSchemaOptions,
 	ParsedObjectsResult,
 	Row
-} from './types.d';
+} from './types.d.js';
 
 export {
 	Schema,
@@ -13,7 +13,7 @@ export {
 	Integer,
 	Email,
 	URL
-} from './types.d';
+} from './types.d.js';

--- a/node/index.d.ts
+++ b/node/index.d.ts
@@ -10,7 +10,7 @@ import {
 	ParseWithoutSchemaOptions,
 	ParsedObjectsResult,
 	Row
-} from '../types.d';
+} from '../types.d.js';
 
 export {
 	ParsedObjectsResult,
@@ -18,7 +18,7 @@ export {
 	Integer,
 	Email,
 	URL
-} from '../types.d';
+} from '../types.d.js';

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.

@catamphetamine
Copy link
Owner

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

@peterhirn
Copy link
Author

peterhirn commented Feb 8, 2023

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".

@catamphetamine
Copy link
Owner

catamphetamine commented Feb 8, 2023 via email

@peterhirn
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants