Skip to content

[dart2js] Produce TypeScript type annotations for compiled files #48947

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
TheOneWithTheBraid opened this issue May 3, 2022 · 1 comment
Closed
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. closed-duplicate Closed in favor of an existing report web-js-interop Issues that impact all js interop

Comments

@TheOneWithTheBraid
Copy link

Dart as of now supports web by using AOT and JIT compiling. The AOT compiler already produces proper source maps.

I would like to propose to extend this to TypeScript-compatible type annotations too.

By using the allowInterloop function from dart:js, it is already possible to interloop a compiled dart library by native JS. Unfortunately, this is not easily implementable with TypeScript because all members are either any (dynamic) or the developers are required to manually create a .d.ts file for type annotations.

I would propose to either add a compiler option to produce corresponding .d.ts files (likely) or to add a two-stage compiler option compiling to TypeScript instead of ECMA script (unlikely).

As both Dart and TypeScript have a similar typing system, the effort should be comparably low.

@mkustermann mkustermann added the area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. label May 4, 2022
@sigmundch
Copy link
Member

Thanks for reaching out and the suggestion!

Note: given that the goal in this bug is similar to #46378 and #37617, I'm marking this as a duplicate.

As you mentioned in your comment - producing valid TypeScript for all Dart code is a complex task. We had discussions about it in the past, especially in the context of the DDC modular compiler, which is less sophisticated than the current optimizing deployment compiler (dart2js). But even there we felt it was a big undertaking.

Unfortunately, even producing .d.ts files is also in tension with other requirements we have. While we support interoperability between the Dart and JS - our design space is to be very intentional of where the interoperability takes place and properly export APIs that are exposed. That allows a smoother evolution of our compilers and enables us to adapt our output representation over time. If every API we generate has a .d.ts file, it will be very hard for us to change calling conventions or even type representations (which for example we are currently doing in some parts of our system).

I should point out that there is one thing that can be done today. It should be possible to generate some of the boilerplate of the JS-interop facade. The js_facade_gen tool, for example, takes .d.ts definitions and can be used to generate dart JSInterop APIs. It is also possible to write code generators that walk the JSInteorp Dart declarations, and generate .d.ts JS files to export the JavaScript API being used.

I sense from your comment that you have a higher need to consume Dart from TS, rather than the other way around. So far, JSInterop makes it easier to consume JavaScript in Dart, but it doesn't make it easy to consume Dart from JavaScript, so the process is more tedious unfortunately :(. This is an area we are about to start looking into as a team.

@sigmundch sigmundch added web-js-interop Issues that impact all js interop closed-duplicate Closed in favor of an existing report labels May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. closed-duplicate Closed in favor of an existing report web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

3 participants