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 enums as const #726

Merged
merged 2 commits into from
Mar 29, 2023
Merged

Export enums as const #726

merged 2 commits into from
Mar 29, 2023

Conversation

srchase
Copy link
Contributor

@srchase srchase commented Mar 24, 2023

Currently, enums as generated as the following:

export enum Suit {
  club = "club",
  diamond = "diamond",
  heart = "heart",
  spade = "spade",
}

This results in the following JavaScript:

var Suit;
(function (Suit) {
    Suit["club"] = "club";
    Suit["diamond"] = "diamond";
    Suit["heart"] = "heart";
    Suit["spade"] = "spade";
})(Suit = exports.Suit || (exports.Suit = {}));

With this PR, enums are now generated as the following:

export const /*enum*/ Suit = {
  club: "club",
  diamond: "diamond",
  heart: "heart",
  spade: "spade",
} as const;
export type Suit = typeof Suit[keyof typeof Suit];

Which results in this JavaScript:

exports.Suit = {
  club: "club",
  diamond: "diamond",
  heart: "heart",
  spade: "spade",
};

By exporting the additional type, compatibility is preserved for any consumers that are using the existing enums as types.

With the tsconfig removeComments setting set to true, the in-line /*enum*/ comment will be excluded from the transpiled JavaScript.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@syall
Copy link
Contributor

syall commented Mar 27, 2023

Are there any backwards compatibility concerns around this?

@srchase srchase force-pushed the enums-as-const branch 2 times, most recently from bb216f2 to 20db8d7 Compare March 28, 2023 20:24
@srchase
Copy link
Contributor Author

srchase commented Mar 28, 2023

Are there any backwards compatibility concerns around this?

I added the type export to allow for using these generated consts as types.

@syall
Copy link
Contributor

syall commented Mar 28, 2023

In the example, it shows a double export of Suit, and was wondering if this is valid?

export const /*enum*/ Suit = { // <- Suit export 1
  club: "club",
  diamond: "diamond",
  heart: "heart",
  spade: "spade",
} as const;
export type Suit = typeof Suit[keyof typeof Suit]; // <- Suit export 2

In TS playground, the output of .d.ts is:

export declare const /*enum*/ Suit: {
    readonly club: "club";
    readonly diamond: "diamond";
    readonly heart: "heart";
    readonly spade: "spade";
};
export type Suit = typeof Suit[keyof typeof Suit];

@srchase srchase merged commit 9a52172 into smithy-lang:main Mar 29, 2023
@srchase srchase deleted the enums-as-const branch March 29, 2023 19:43
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.

4 participants