Skip to content

detection of definition files in typescript modules does not follow npm #11672

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

Closed
sramam opened this issue Oct 16, 2016 · 4 comments
Closed

detection of definition files in typescript modules does not follow npm #11672

sramam opened this issue Oct 16, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@sramam
Copy link

sramam commented Oct 16, 2016

TypeScript Version: 2.0.3

Actual behavior:
The TypeScript compiler option --declaration produces definition files that mirror the source.ts structure and honor the tsconfig.json settings.

npm uses the "main" field in package.json to identify entry point to the module.
This allows substantial flexibility in how source and distribution directory structures are organized.

However, when such a module is included in a different TypeScript module, the TypeScript compiler seems to require an index.d.ts be present in the base directory of the module.
This can be as simple as a one-liner: export * from './dist/index;, but it's still a departure
from default npm behavior.

Expected behavior:
TypeScript should seek the default definition file that mirrors the "main" field of package.json

Code

With a directory structure that looks like this:

├─ src/
|  └── index.ts
├─ dist/
|  ├── index.d.ts
|  └── index.js
├── package.json
└── tsconfig.json

And the package.json like this:

...
"main": "dist/index.js"
...

For this module to be importable in other typescript modules, TypeScript expects a root index.d.ts to be present like this:

├─ src/
|  └── index.ts
├─ dist/
|  ├── index.d.ts
|  └── index.js
├── package.json
└── tsconfig.json
├── index.d.ts  <<<--------- 

@blakeembrey
Copy link
Contributor

This would be a heavy breaking change at this point, a lot of Typings projects follow the structure and rely on TypeScript not picking it up without an explicit reference in package.json. For reference, maybe you're after https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html which has documentation on how to point to dist/index.d.ts (by using typings or types in package.json).

@yortus
Copy link
Contributor

yortus commented Oct 17, 2016

nit: the behaviour of "main" is a Node.js standard (docs here), not an npm thing. npm just conforms to Node.js standards, and the question here is really whether TypeScript should conform to this Node.js standard too.

@sramam
Copy link
Author

sramam commented Oct 17, 2016

@yortus good to learn. thanks.

@blakeembrey thanks for the link. I missed the "types" field in my (non!) reading of the manual and spent many hours chasing my tail. 😬

Especially given the --declaration compiler option which behaves without surprise, this strikes me as odd. I can't think of a use-case when it is advantageous to not have a type-definition file mirror the entry-point to a module. Would you have an example?

@mhegazy
Copy link
Contributor

mhegazy commented Oct 17, 2016

This is a duplicate of #9854. #9854 is marked as "Accepting PRs", so feel free to send a PR for it.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 17, 2016
@mhegazy mhegazy closed this as completed Oct 17, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants