-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Recommendation for exposing multiple TypeScript modules from single NPM package #8305
Comments
We need to add documentation, so leaving this issue open to track that, and marking it as such. For the question, the recommendation is to generate the .d.ts with 1:1 mapping to your .js files, and place them next to the .js file in the published package, as you mentioned. the I have a sample posted at https://github.com/mhegazy/npm-dependency-test, take a look and let me know if you have more questions. |
Thanks @mhegazy One option to avoid the pollution is to use a As you suggested, it would be great to update the handbook on best practices for distributing typescript built libraries, especially with respect to typings and dependent types. |
@mhegazy I'm confused about why there is the need for a separate "typings" entry when the .d.ts is invariably next to the one referenced by "main". Why not simply look next to the "main" .js or at least look there as well? |
Not sure i see the issue really. if you are adding |
@mhegazy It's more that is seems inconsistent. From my understanding, if you: import foo = require("foo.js"), TypeScript tools will automatically look for type definitions in "foo.d.ts". If however you: require("bar") and there is a "main": "foo.js" in bar/package.json, TypeScript tools won't automatically look for type definitions in "bar/foo.d.ts". This is a shame because otherwise you could convert a JavaScript package into TypeScript by simply renaming the .js files to .ts and compiling with the definitions option. Unfortunately you need to know about the "typings" option otherwise your consumers won't see your type definitions. It just seems like an unnecessary gotcha. Why not support "main" relative as well as "typings"? Does that make sense? |
it does work for index.js -> index.d.ts. We could add support for |
How about making it so that if |
feel free to file a separate issue for this proposal. |
This will definitely be a major improvement for Typescript development. I'm currently developing a library in TS and this library should be included in an Angular 2 application. I've also looked into angular repository to see how they are exporting the modules, but it seems that they bundle the modules in a single file. |
here is the documentation for writing declaration files. please let us know if there are any missing scenarios. |
Hi @mhegazy and thank you for your answer. Where can I find the documentation ? |
Apologies. forgot to add the link. here you go: https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/declaration%20files/Introduction.md also fixed the original comment. |
My usecase is the following, my project builds and output the generated d.ts files to
and I then in my package.json have specificed that From consumer project the following work and it consumes the
but when doing
it cannot find the Copying stuff to |
@pksorensen I'm using the same workflow, but I'm also finding it not so elegant and clean. It will be nice to have a method/process that allows you can export a single file (or archive) and than you're good to go. The developers that have already worked with JAVA may relate this to a library export as a .jar file. |
I found a better approach. In my consumer project wiht typescript 2.0 I could set it up to resolve submodules. See the compile time section of readme at https://github.com/s-innovations/kolayout , where setting path compile option of tsc to resolve "mylib/*" to the dist folder of d.ts files of my mylib. |
This is following advice found in this comment: microsoft/TypeScript#8305 (comment)
This is following advice found in this comment: microsoft/TypeScript#8305 (comment)
I have the same flow as described in #8305 (comment) That is, using a subfolder for generated files (.js and .d.ts). When you do that, and you have:
Setting the
However, this still fails:
(Of course this works If I'm reading above right, this is supposed to work if I'm compiling to the same source directory. Is there a way to make it work if I compile to a sub-directory? |
no there is not currently.. We could in theory do multiple lookups if |
Almost four years later and still no practical solution to this? 🤔 |
Any solution for this frustrating problem? |
Ditto ☝️ Would be nice to be able to do this |
This used to be a fundamental restriction of Node.js, but recently there's added support for the TypeScript support is tracked in #33079. |
This package exports multiple modules which are intended to be required separately. This is a bit tricky with TypeScript: microsoft/TypeScript#8305 microsoft/TypeScript#33079 The only way to make this work is to get rid of the "./src" and "./dist" directories and dump everything at the top level. Ick.
|
I've also spent a lot of time today trying to figure out why my declaration files are not loaded. I was compiling my declarations to the I guess it would be very reasonable to expect from TypeScript to look for declarations relative to the directiry/file specified in I can't quite comprehend why this issue is closed without a solution… |
@slavafomin, the same... I have reproduced this bug (I think it is disagreeable bug...) with UPD: Well, I saw, that dev-dependencies are not being indexed on auto-suggesting. Thats the another issue. Maybe installing your package as dependency (not dev-dependency) will help you.. |
Thanks to @chriskrycho for the very clear description of the situation and workaround in his article linked from #8305 (comment) The fact that the TS compiler cannot resolve submodule definitions that were generated and positioned within the package structure by the TS compiler itself is pretty remarkable. As a case of the TS compiler not being able to consume its own output, it could be viewed as an internal inconsistency. My use case is providing a submodule with interfaces intended to be augmented (merged) via ambient declaration. This is itself a workaround for TS lack of higher-kinded types. I get why the HKT thing is difficult, but this submodule resolution issue seems on the surface like it should be much more tractable. |
I'm having same problem while using one package for both npm publication and for using as submodule package as my other typescript project. specifying main should point index.ts file and tsc or yarn/npm should somehow automatically link to dist/index.js or perhaps this should be another thread fro npm & package.json? This is a huge pain since we share a lot of project as submodule & for both npm in more thatn 100+ repos |
#33079 seems to be fixed, but I still can't find a great working example of a library with submodules hierarchy set up. Anyone has good references? |
I have spent hours trying to configure a repo to work with submodule types. The repo has a build tool, rollup, and a folder structure that doesn't reflect the submodule importing scheme. #8305 (comment) has pointed out rxjs so I looked at the config they've done since they manage to expose typed submodules with a build tool and w/o mirroring the submodule importing scheme as suggested in the official guide. Related: #17945 FindingsIt seems that though TS doesn't respect the Hope this helps someone. |
The module resolution logic in the handbook doesn't mention how nested modules are supported from a single npm package. There is an old issue discussing this that has been closed: #5804
Neither the solution here or simply trying to refer to modules beneath the root dir referred to by the typings or main path seems to work. Is there a documented best practice? The documentation says that ambient modules should not be used in the typings field in package.json.
Just to be clear, I want to write this in a consuming app:
Where the package.json file in my-npm-lib has:
And the file structure in the my-npm-lib:
Currently this complains with a module not found error. But this works, but shouldn't:
This is all with the module resolution setting set to node.
The text was updated successfully, but these errors were encountered: