Skip to content

[interop] Add Support for Enums #404

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

Merged
merged 6 commits into from
Jul 8, 2025
Merged

Conversation

nikeokoronkwo
Copy link
Contributor

@nikeokoronkwo nikeokoronkwo commented Jul 1, 2025

Closes #393 and #384

This PR adds support for enumerations in the Dart JS Interop Interface Gen project.
Enums in TS with the enum keyword are mapped to (constant) Dart extension types with the values being static properties.
Therefore:

export declare enum Direction {
  Up = 0,
  Down = 1
}

Turns into:

extension type const Direction._(num _) {
  static const Direction Up = Direction._(0);
  static const Direction Down = Direction._(1);
}

This enum pattern is used to represent TS Syntax Kinds in the generator.
Although the vast majority of enums declared with the enum keyword are homogenous with defined number or string values:

  • If an enum value is computed and does not have a default value, it is declared external (and the rep type becomes a JS-compatible rep type)
  • If an enum is heterogenous, then the rep type becomes JSAny
  • If the rep type of an enum is a JS type, then the enum properties can no longer be constant due to literal conversions.

This PR also supports homogenous union types as enums (i.e "north" | "south" | "east" | "west"), translating them to become enum declarations.

@nikeokoronkwo nikeokoronkwo marked this pull request as ready for review July 2, 2025 04:10
@nikeokoronkwo nikeokoronkwo requested a review from srujzs July 8, 2025 01:29
Copy link
Contributor

@srujzs srujzs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just two more nits, sorry :)

@srujzs
Copy link
Contributor

srujzs commented Jul 8, 2025

Thanks Nike!

@srujzs srujzs merged commit f0f0914 into dart-lang:main Jul 8, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interop Gen: Support Enumerations
2 participants