Skip to content

Commit

Permalink
fixes #65 - enum as object key
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 24, 2023
1 parent 78c07c7 commit 32d7de4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lang/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,18 @@ fn datatype_inner(ctx: ExportContext, typ: &DataType) -> Result<String, TsExport
DataType::Literal(literal) => 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<K, V>` to avoid issues with circular references.
"{{ [key: {}]: {} }}",
"{{ [key{divider} {}]: {} }}",
datatype_inner(ctx.clone(), &def.0)?,
datatype_inner(ctx, &def.1)?
)
Expand Down
11 changes: 11 additions & 0 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{
cell::RefCell,
collections::HashMap,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
path::PathBuf,
};
Expand Down Expand Up @@ -156,6 +157,9 @@ fn typescript_types() {
// https://github.com/oscartbeaumont/specta/issues/66
assert_ts!([Option<u8>; 16], r#"(number | null)[]"#);

// https://github.com/oscartbeaumont/specta/issues/65
assert_ts!(HashMap<BasicEnum, ()>, r#"{ [key in "A" | "B"]: null }"#);

// assert_ts_export!(DeprecatedType, "");
// assert_ts_export!(DeprecatedTypeWithMsg, "");
// assert_ts_export!(DeprecatedFields, "");
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 32d7de4

Please sign in to comment.