From 32d7de42b21c9c6b296279f02c36ea61e0f96975 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Mon, 24 Apr 2023 15:21:44 +0800 Subject: [PATCH] fixes #65 - enum as object key --- src/lang/ts/mod.rs | 11 ++++++++++- tests/ts.rs | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lang/ts/mod.rs b/src/lang/ts/mod.rs index 792a550..7cb4018 100644 --- a/src/lang/ts/mod.rs +++ b/src/lang/ts/mod.rs @@ -153,9 +153,18 @@ fn datatype_inner(ctx: ExportContext, typ: &DataType) -> Result literal.to_ts(), DataType::Nullable(def) => format!("{} | null", datatype_inner(ctx, def)?), DataType::Record(def) => { + let divider = match &def.0 { + DataType::Enum(_) => " in", + DataType::Named(dt) => match dt.item { + NamedDataTypeItem::Enum(_) => " in", + _ => ":", + }, + _ => ":", + }; + format!( // We use this isn't of `Record` to avoid issues with circular references. - "{{ [key: {}]: {} }}", + "{{ [key{divider} {}]: {} }}", datatype_inner(ctx.clone(), &def.0)?, datatype_inner(ctx, &def.1)? ) diff --git a/tests/ts.rs b/tests/ts.rs index 49e6c6f..1d2ff14 100644 --- a/tests/ts.rs +++ b/tests/ts.rs @@ -2,6 +2,7 @@ use std::{ cell::RefCell, + collections::HashMap, net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}, path::PathBuf, }; @@ -156,6 +157,9 @@ fn typescript_types() { // https://github.com/oscartbeaumont/specta/issues/66 assert_ts!([Option; 16], r#"(number | null)[]"#); + // https://github.com/oscartbeaumont/specta/issues/65 + assert_ts!(HashMap, r#"{ [key in "A" | "B"]: null }"#); + // assert_ts_export!(DeprecatedType, ""); // assert_ts_export!(DeprecatedTypeWithMsg, ""); // assert_ts_export!(DeprecatedFields, ""); @@ -388,6 +392,13 @@ pub struct NonTypeType; #[serde(transparent)] pub struct TransparentTypeWithOverride(#[specta(type = String)] NonTypeType); +#[derive(Type, serde::Serialize)] +#[specta(export = false)] +pub enum BasicEnum { + A, + B, +} + // #[derive(Type)] // #[specta(export = false)] // #[deprecated]