Skip to content
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

Export type definitions #48

Closed
SonofNun15 opened this issue Sep 23, 2015 · 14 comments · Fixed by #128
Closed

Export type definitions #48

SonofNun15 opened this issue Sep 23, 2015 · 14 comments · Fixed by #128

Comments

@SonofNun15
Copy link

If I try to bundle a typescript project into a commonjs library for reuse, webpack with ts-loader will spit out beautiful javascript commonjs code for the library, but I have to use tsc in order to generate the declaration files.

Have you considered trying to support export of type definitions next to a bundle via ts-loader?

For reference:
https://github.com/SonofNun15/typescript-library-test/blob/master/README.md

@SonofNun15
Copy link
Author

By the way, ts-loader is awesome. I am so happy with webpack and ts-loader right now. This is the closest we have come to finally resolving our TS library issues! Thanks for all of your work! 😄

@jbrantly
Copy link
Member

This is related to #45. Basically it probably wouldn't be crazy hard to support --declaration like tsc does where each input file gets an associated declaration file. But where things get hard is with having a single declaration file for a multiple modules. I'm not sure that is in-scope right now. I think it would be best to somehow combine with another tool that already does that or wait until TypeScript gets native support for it.

I'm open to exploring the simple case, but does that actually solve your problem?

Also, thank you for the kind words 😁

@SonofNun15
Copy link
Author

I totally understand. Combining multiple external modules into a single file (module) is kind of a hairy problem no matter how you cut it. I agree, let's see how the TS team handles it first. I think this will ultimately only have value if we intended to code split and deploy the library dynamically to the client. At this point we'll probably be bundling it with the client (using webpack / ts-loader), so this won't be an issue.

@SonofNun15
Copy link
Author

Resolution was to wait and see what the TypeScript team does with export of type definitions for external modules.

@flyon
Copy link

flyon commented Dec 13, 2015

@jbrantly I think supporting --declaration for now would be nice. At least then we can generate single .d.ts files, which with other modules can be combined into one. That would solve my use-case for now at least.

@cspotcode
Copy link
Contributor

I would like this as well. If I can just get access to the .d.ts EmitOutput from within a loader, I can output the .d.ts files myself. Simple loaders and plugins are so easy to write, I don't mind putting a few lines of code together, and you can still wait to implement proper .d.ts bundling.

@jbrantly
Copy link
Member

Yup, going to reopen this to track adding individual file .d.ts output so at least other tools can consume them.

@flyon
Copy link

flyon commented Dec 23, 2015

Great!

@jbrantly
Copy link
Member

This functionality has been merged to master. Please try it out and let me know how it works out.

@cspotcode Just for fun I tried your scenario of accessing the .d.ts files from another loader. Unfortunately I was not successful. It seems that when a loader adds extra files to the output those files are not made available to other loaders. (if you dig deeper and figure out a way to do this I'd love to hear about it)

@flyon
Copy link

flyon commented Jan 14, 2016

@jbrantly tnx! I downloaded the master branch and succesfully got separate .d.ts output files. Could you publish this to npm?

I played around a while with the different options of combining declaration files (like dts-generator), but ended up writing my own small WebpackPlugin in typescript because I want to combine the files in a certain way. It's defenitly not usable as-is for others, but if anyone wants to see how to do that check the npm library dacore-webpack-declarations

Other usefull information in this context: Typescript 1.8 will support generating one single declaration file for certain types of modules. The combination of compiler options outFile and declarations for AMD and system modules will generate a single output file with a single declaration file. You can already try it out with npm typescript@next

@jbrantly
Copy link
Member

Apologies for the delay, this has been published in v0.8.0.

@pillowsoft
Copy link

I also tried it and it did generate the definitions 👍. However, I ran into one issue and wanted to see if you had thoughts on a fix. I like to create files that just have interface definitions that are shared and then import those definitions into the files that need the types. Alas, when I do that, no type definitions are generated for those files. Typescript correctly generates them using "declaration": true, but ts-loader is not. Perhaps ts-loader somehow sees no code is in the file and then does not generate the definition?

Here's an example of a type only file:

export interface IImageInfo {
    src: string; 
    thumbnail?: string; 

    description?: string;
    originalClass?: string;
    thumbnailClass?: string;
    srcset?: Array<any>;
    caption?: string;
}

Thoughts? Thanks!

@rob-bateman
Copy link
Contributor

I'm also trying to generate definitions files for interfaces from ts-loader, but ran into the same problem. Had a look at the declaration-bundler-webpack-plugin by @flyon but don't see any interface files coming from the compiler 'emit' callback. Any suggestions?

@rob-bateman
Copy link
Contributor

created issue (#174) and pull request (#175) for resolving missing d.ts files for interface objects referenced by @pillowsoft

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants