tsd icon indicating copy to clipboard operation
tsd copied to clipboard

Add support for "typings" property in package.json

Open rbuckton opened this issue 8 years ago • 9 comments

In TypeScript 1.6 we are including a new module resolution semantics for CommonJS modules, which can be explicitly controlled using the new --moduleResolution command line option. Now, when you use --module commonjs or specify --moduleResolution node, module resolution for module names follows a new resolution strategy, based on the module resolution semantics for NodeJS.

When an absolute module name is resolved from the node_modules/ folder, an attempt is made to locate and parse the package.json file. TypeScript looks for a specific property, "typings" on the root of the package object.

This differs from how tsd currently handles type definitions embedded in package.json, which instead looks for a "typescript" property that contains an object with a "definitions" property.

Please consider adding support for the "typings" property, with the "typescript" object as a backwards-compatible fallback.

rbuckton avatar Aug 27 '15 20:08 rbuckton

@rbuckton Thanks so much for this! What is the expected definition format for typings in 1.6? Can you use modules directly from the compiled output now? This is a pretty big win for TypeScript users, thanks for the update!

How does this work with "ambient modules" (is there a better name?) such the node definition itself? Basically modules that reference other modules but don't export anything themselves.

Edit: Will this work with other package managers like Bower?

blakeembrey avatar Aug 27 '15 20:08 blakeembrey

@blakeembrey When loading from the node_modules/ folder, this must be a declaration file. As far as any of your other questions about how module resolution works, I'd have to loop in @vladima.

rbuckton avatar Aug 27 '15 20:08 rbuckton

Ok, thanks. Just trying to find the diff between what I've currently built/solved for the TSD future versions and what is coming up. Don't want to duplicate too much effort.

Currently, my solution is to use a combination of a typings map and an array for "ambient" definitions. I use the TypeScript compiler to then generate a single file for each dependency, with sub-dependencies inlined, that is added to the typings directory and I'm looking at using tsconfig.json for holding this information (under something like tsd.{dependencies,ambientDependencies}. This way, the output can be directly from the TypeScript compiler checked into NPM and people don't have to always write declaration files (it's possible for aliases of modules to happen, etc.).

By the sounds of it, there is a need for this to still exist. Can you point me to the implementation in TypeScript so I can start resolving what is now unnecessary?

blakeembrey avatar Aug 27 '15 20:08 blakeembrey

This change is in the release-1.6 branch on the TypeScript GitHub, and available to test via typescript@next on npm.

rbuckton avatar Aug 27 '15 20:08 rbuckton

We just finished migrating angular to this, and will roll it out tomorrow. https://docs.google.com/document/d/1jiB1SHfM2AXacKdTbQBoPyKlp7ZSH-LBbfc5gzeMKVY/edit# I am very excited about how easy this makes it for our users.

I believe users should not use tsd for angular2. tsd and DT are the side-channel to deliver typings for libraries that don't distribute their own, but we have better control over things like versioning semantics if we publish the typings in our primary distribution.

My choice of right behavior for tsd is to

  • check if the project already has an npm installation for the tsd typing requested. if so, log to the console "you already have typings for angular2 installed by npm, so there is no need to add angular2 to your tsd.json". Users should expect to be able to compile against the library directly.
  • otherwise, log to the console: "this package distributes typings in its NPM package, but it doesn't seem to be installed. Please npm install angular2"

alexeagle avatar Oct 11 '15 22:10 alexeagle

Thanks @alexeagle, I've already added support for the typings field in the future branch and I had sent an email to try to have this discussion at the beginning of the week to Misko. If you want to discuss it, I'd love to jump on a call or something, but I don't think it makes sense to implement it like that. I actually read that at the beginning of the week too, and wanted to get a chance to properly discuss it.

blakeembrey avatar Oct 11 '15 23:10 blakeembrey

Yes, we would love to chat with you guys. Tsd remains really important for our users to be productive in Typescript. We are all really busy getting ready for Angular Connect, so I'm not sure we'll find time this week. If not, maybe the week after the conference. Are you in San Francisco?

alexeagle avatar Oct 12 '15 02:10 alexeagle

Yes, I actually work just off Market Street. Feel free to email if you want to move this discussion off here though :smile:

blakeembrey avatar Oct 12 '15 03:10 blakeembrey

I emailed you at the address shown on github.

alexeagle avatar Oct 12 '15 18:10 alexeagle