-
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
Suggestion: package declaration file for commonjs packages #3089
Comments
👍 |
I've updated the branch in a way that differs slightly from the above proposal, in that |
Just curious how this would deal with Node.js modules whose public APIs include types from other Node.js modules. E.g.:
|
That's an area I am still investigating. Right now it depends on how it is referenced in the project. I imagine something like tsd would be useful here. |
So if I understood correctly, a scenario might go something like this:
The thing I'm not sure about is the concept of the 'file list'. Is that from |
@yortus I don't have a recommendation or best practice yet. I'll have to think on this some this week. The problem with reference tags is that the paths end up fixed into the declaration file. If you ship a declaration file with these reference tags, you must be 100% sure that those paths will be the same ones that the consumer of the declaration file will use. For common declarations like node.d.ts, there's no guarantee that the consumer will want to use the same path, much less the same version of the declaration. By file list, I meant the files either supplied to tsc on the command line or through tsconfig.json. |
@rbuckton OK cool, looking forward. Yeah the relative reference paths thing is tricky. Definitely Typed has a consistent convention, but its not the only convention out there. |
I stumbled on this today. Edit Removing my proposal, there is proper work going on with this in here: #2839 see this branch: https://github.com/Microsoft/TypeScript/compare/moduleResolution |
Using the global |
this should be handled today using |
before module resolution logic picked node packages, there was no way to define a node package except in an ambient declaration, this should not be needed now. bundling module declarations in one file is supported for System and AMD, but not node, as node packages do not need to be bundled. if this is something you need please elaborate on the details of the scenario. |
Proposal
To better support the development and consumption of node.js modules in TypeScript, I propose that we add the ability to emit a single declaration file for a node.js package. To that end, I suggest we add the following features:
packageName
compiler option, used to specify the name of the node.js package.packageMain
compiler option, used to specify the path to the main typescript module for the package.packageDeclaration
compiler option, used to specify the output path of the declaration file for the package.declaration
and eithermodule
or atarget
ofES6
, we should emit a single declaration file for the program inputs.The output for a
packageDeclaration
would have the following form:packageName
.package.json
file, common directory path, or the current directory.Example
Given the following sources:
a.ts
lib\b.ts
index.ts
And the following command line:
You would get the following package declaration output file:
sample.d.ts
Out of scope
It would be nice to also be able to emit only the exports visible from the main module, and reduce the overall output side of the declaration file, but not all node.js packages are designed to work that way. Instead, in the future we could investigate an option to choose whether to emit a single ambient module declaration. The proposed approach can be used whether you intend to have a single output module or the ability to reach nested submodules, while the more restrictive approach would alienate some package authors.
It is also out of scope to infer package information from the
package.json
file, although that may be considered in the future. That might look something like having apackage
compiler option that can read thepackage.json
file to infer thepackageName
from the"name"
field of the package, and thepackageDeclaration
from the"typings"
(proposed) field of the package. However, its more complicated to derivepackageMain
as the"main"
field of the package will be pointing to the output file and not the original typescript file. We might have to propose a"devMain"
, or someother similarly named field, to satisfy this requirement.
As proposed, using
packageDeclaration
will circumvent regular declaration file generation. We could opt to support both, by writing individual declaration files for each non-declaration input as well as the package-level declaration, though I am not certain whether that would be necessary.A sample implementation for this proposal can be found in the packageDeclaration2 branch.
The text was updated successfully, but these errors were encountered: