#![allow(dead_code)] use std::collections::{BTreeMap, HashMap, HashSet}; use ts_rs::{Config, TS}; #[derive(TS)] #[ts(export, export_to = "hashmap/")] struct Hashes { map: HashMap, set: HashSet, } #[test] fn hashmap() { let cfg = Config::from_env(); assert_eq!( Hashes::decl(&cfg), "type Hashes = { map: { [key in string]: string }, set: Array, };" ) } struct CustomHasher {} type CustomHashMap = HashMap; type CustomHashSet = HashSet; #[derive(TS)] #[ts(export, export_to = "hashmap/")] struct HashesHasher { map: CustomHashMap, set: CustomHashSet, } #[test] fn hashmap_with_custom_hasher() { let cfg = Config::from_env(); assert_eq!( HashesHasher::decl(&cfg), "type HashesHasher = { map: { [key in string]: string }, set: Array, };" ) } #[derive(TS, Eq, PartialEq, Hash)] #[ts(export, export_to = "hashmap/")] struct CustomKey(String); #[derive(TS)] #[ts(export, export_to = "hashmap/")] struct CustomValue; #[derive(TS)] #[ts(export, export_to = "hashmap/")] struct HashMapWithCustomTypes { map: HashMap, } #[derive(TS)] #[ts(export, export_to = "hashmap/")] struct BTreeMapWithCustomTypes { map: BTreeMap, } #[derive(TS)] #[ts(export, export_to = "hashmap/")] enum EnumKey { Foo, Bar, } #[test] fn with_custom_types() { let cfg = Config::from_env(); assert_eq!( HashMapWithCustomTypes::inline(&cfg), BTreeMapWithCustomTypes::inline(&cfg) ); assert_eq!( HashMapWithCustomTypes::decl(&cfg), "type HashMapWithCustomTypes = { map: { [key in CustomKey]: CustomValue }, };" ); assert_eq!( HashMap::::name(&cfg), "{ [key in EnumKey]?: string }" ); assert_eq!( HashMap::::inline(&cfg), r#"{ [key in "Foo" | "Bar"]?: string }"# ); }