thepassle / app-tools

125 stars 8 forks source link

The TS declaration files are not found automatically #22

Closed andyjessop closed 10 months ago

andyjessop commented 10 months ago

I've just installed the router with npm i -S @thepassle/app-tools. I copy/pasted the example from the router readme and am getting the following TS errors:

Could not find a declaration file for module '@thepassle/app-tools/router.js'. '/[project]/node_modules/@thepassle/app-tools/router.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/thepassle__app-tools` if it exists or add a new declaration (.d.ts) file containing `declare module '@thepassle/app-tools/router.js';`ts(7016)
thepassle commented 10 months ago

You'll need a tsconfig that is able to resolve package exports, for example a moduleResolution of bundler or node16. You can find a working example here: https://github.com/pwa-builder/pwa-starter

andyjessop commented 10 months ago

Unfortunately that brings its own issues. I would have to convert the whole project to type: module and then use the .js extension for all relative imports.

thepassle commented 10 months ago

I would have to convert the whole project to type: module

I dont think thats true, thats not the case in pwa-builder either

andyjessop commented 10 months ago

Ah actually, Bundler does work fine (but Node16 gives this error:

Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean ...

Thanks for your help. I'll do some reading on Bundler.

andyjessop commented 10 months ago

It might be worth adding a section about this in the README.

thepassle commented 10 months ago

It might be worth adding a section about this in the README.

Yeah, thats a good point. Ill try to update the docs about this soon.

I'll close this issue now, please feel free to reopen if you run into any other problems :)

andyjessop commented 10 months ago

@thepassle I tried to PR changes to the docs, but I don't have permission:

Screenshot 2023-11-23 at 21 22 51

Here is the section I tried to add to the README:

## Using `@thepassle/app-tools` with TypeScript

In order for your project to see the provided type declaration files, you may need to modify your `tsconfig.json`. Two primary configurations can resolve these issues:

1. **`moduleResolution: node16` or `moduleResolution: nodenext**: 
   - Modern versions of Node.js (v12 and later) support ECMAScript imports and CommonJS `require`, resolving them using different algorithms. 
   - **This approach requires explicit `.js` file extensions in TypeScript imports.**
   - Suitable for projects aligned with modern Node.js module resolution strategies.

2. **Using `moduleResolution: bundler`**: 
   - Ideal for projects utilising bundlers.
   - Supports `package.json` "imports" and "exports" without requiring file extensions on relative paths.
   - Note: `bundler` does not support resolution of `require` calls in TypeScript files.

### Further Reading
For more detailed information on `moduleResolution` configurations, please refer to the [TypeScript documentation](https://www.typescriptlang.org/tsconfig#moduleResolution).
thepassle commented 10 months ago

Cool, thanks! Ill add this to the readme tomorrow