-
Notifications
You must be signed in to change notification settings - Fork 304
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
Can Fable generate .d.ts TypeScript declarations for generated code? #270
Comments
This feature has already been requested but I haven't had the time yet to implement it, I'll try to do it in the next few weeks. Having it as a pending issue will make for a nice reminder 😉 Note that the |
I think building a prototype for this would be a fun project that someone from the community could start working on. In case someone wanted to send a PR 😉 the Fable compiler already walks over the typed AST from F# (I guess this is happening here), so the change would just have to do the same thing and turn the type info into a |
I'd love if someone could do do that but maybe it's a bit complicated for someone without a deep understanding of how F# and Fable AST work ;) I think it shouldn't be too complicated but I still need to have look at it. I was thinking that maybe it's easier to generate the |
Well. It's a rainy weekend around here ;) #277 adds a very-very-very-first implementation of this. As it stands now, it's unusable for any practical purposes. But enough to raise some questions. Using it to compile the current
Open questions / problems to solve / todo list:
|
Fantastic work! 👏 I need to have a deeper look later. For now, I'll try to answer your questions:
|
Another push to #277. Adds initial class declaration (very simple cases, far from complete). I did notice the problem with interfaces. Maybe the code is too "late" in the transformation pipeline? But I'll wait for your considerations. Some more problems:
Moving the code to an earlier step in the compilation process maybe would help. But it will probably require some deeper changes (embedding it in Also, I did some tests trying to output one single
instead of
In the first case, every
I will be glad to hear from more experienced TypeScript users about this subject. |
Personally I would start with something very simple to make it usable from TypeScript (even if it's just listing the method names) and use I need to check better how TypeScript discovers the declarations (in the last case, the user can just include a import * as Util from './util';
import * as AnotherModule from './anothermodule'; In declare module './util' {
export ...
}
declare module './anothermodule' {
export ...
} Check this declaration for example. |
A single |
No, but it can be done with a bundler like Webpack. Anyways, as mentioned above, if I'm not mistaken a single |
Whatever you guys decide as to if use 1:1 or 1:many, I would recommend taking a look at the official line on typings for npm packages. My understanding is that the best way to handle this is with 1 per file (1:1), and to simply let the resolver sort out the rest. This prevents you needing to use named exports, which are brittle and incompatible with non-aliased paths. I appreciate that people are not necessarily using npm for distributing their transpiled F# packages, but if it is of interest, I did create a really dumb typescript sample here which demonstrates how to package up .d.ts files with your node module. No more tsd yay :) |
@alfonsogarciacaro: Yes. A single Using a 1:1 @Metal10k: Yes. Latest |
Ok, if everybody thinks one declaration per file is the way to go we should do that 👍 I've been investigating more about this, and probably it's better to focus our efforts to improve Fable and Babel AST and add type annotations to the generated code. Then use the Babel DTS generator to create the declarations from the annotated JS code. Unfortunately, type annotations don't appear in Babel AST spec. However, Babel actually accepts them as it can be checked in the AST explorer. |
fable-compiler 0.5.0 already outputs Please give it a try and tell me what you think. |
This is pretty awesome :) One minor issue i found with 0.5.1 is union types seem to catastrophically break.
|
Damn, this is node again finding the plugin somewhere else in my computer and confusing me. This only happens if you pass the Don't worry about the ticket, it's a very easy one. So I'll include it with other minor fixes 👍 |
Yes only when --declaration is used, otherwise it compiles fine. Cool, nice one 👍 |
@Metal10k, I've released [email protected] adding the babel-transform-class-properties plugin. Could you please give it a try to see if it works now? Thanks in advance! |
Works perfectly, Cheers! |
Thanks for the confirmation! Any comments about the process of generating and using the declarations are welcome (though please note that, as commented above, there are still a few things missing). It'd be also great if you could write a post explaining users about the new feature 👍 |
Description
I have a mature TypeScript codebase and would like to introduce Fable. In order to call Fable-generated code, I would like to have
.d.ts
files for the generated Fable code.The text was updated successfully, but these errors were encountered: