From a2f2d54e439c8263d811ba276865bd7ad131c3e6 Mon Sep 17 00:00:00 2001 From: Asraelite Date: Mon, 28 Jun 2021 21:27:07 +0200 Subject: [PATCH] Assign explicit values to TS enums (#24) * 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 --- crates/gen-js/src/lib.rs | 6 +++--- crates/gen-js/tests/run.ts | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/gen-js/src/lib.rs b/crates/gen-js/src/lib.rs index b9f74aa70..5976cc843 100644 --- a/crates/gen-js/src/lib.rs +++ b/crates/gen-js/src/lib.rs @@ -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"); diff --git a/crates/gen-js/tests/run.ts b/crates/gen-js/tests/run.ts index 53e76c5c4..078317dd9 100644 --- a/crates/gen-js/tests/run.ts +++ b/crates/gen-js/tests/run.ts @@ -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 },