Skip to content

Commit

Permalink
Assign explicit values to TS enums (#24)
Browse files Browse the repository at this point in the history
* Assign explicit values to TS enums

TypeScript enums without explicitly assigned values are difficult to use if they are defined in declaration files.

* Test TS enum variants as types
  • Loading branch information
Asraelite committed Jun 28, 2021
1 parent dd234ab commit a2f2d54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions crates/gen-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ impl Generator for Js {
} else if variant.is_enum() {
self.src
.ts(&format!("export enum {} {{\n", name.to_camel_case()));
for case in variant.cases.iter() {
for (i, case) in variant.cases.iter().enumerate() {
self.docs(&case.docs);
self.src.ts(&case.name.to_camel_case());
self.src.ts(",\n");
let name = case.name.to_camel_case();
self.src.ts(&format!("{} = {},\n", name, i));
}
self.src.ts("}\n");

Expand Down
5 changes: 5 additions & 0 deletions crates/gen-js/tests/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ function test_variants(wasm: exports.Wasm) {
assert.deepStrictEqual(wasm.invert_bool(true), false);
assert.deepStrictEqual(wasm.invert_bool(false), true);

{
const a: exports.E1.A = exports.E1.A;
const b: exports.E1.B = exports.E1.B;
}

{
const [a1, a2, a3, a4, a5, a6] = wasm.variant_casts([
{ tag: 'a', val: 1 },
Expand Down

0 comments on commit a2f2d54

Please sign in to comment.