Skip to content

Commit ac758f2

Browse files
tfausakthomasjm
authored andcommitted
Fix type for maps with non-string keys
1 parent 1ea2172 commit ac758f2

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/Data/Aeson/TypeScript/Instances.hs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE OverlappingInstances #-}
55
{-# LANGUAGE CPP #-}
66
{-# LANGUAGE PolyKinds #-}
7+
{-# LANGUAGE TypeApplications #-}
78
{-# OPTIONS_GHC -fno-warn-orphans #-}
89

910
-- Note: the OverlappingInstances pragma is only here so the overlapping instances in this file
@@ -171,13 +172,18 @@ instance (TypeScript a) => TypeScript (Maybe a) where
171172
instance TypeScript A.Value where
172173
getTypeScriptType _ = "any";
173174

174-
instance (TypeScript a, TypeScript b) => TypeScript (Map a b) where
175-
getTypeScriptType _ = "{[k in " ++ getTypeScriptKeyType (Proxy :: Proxy a) ++ "]?: " ++ getTypeScriptType (Proxy :: Proxy b) ++ "}"
176-
getParentTypes _ = [TSType (Proxy :: Proxy a), TSType (Proxy :: Proxy b)]
177-
178-
instance (TypeScript a, TypeScript b) => TypeScript (HashMap a b) where
179-
getTypeScriptType _ = [i|{[k in #{getTypeScriptKeyType (Proxy :: Proxy a)}]?: #{getTypeScriptType (Proxy :: Proxy b)}}|]
180-
getParentTypes _ = L.nub [TSType (Proxy :: Proxy a), TSType (Proxy :: Proxy b)]
175+
instance (TypeScript a, TypeScript b, A.ToJSONKey a) => TypeScript (Map a b) where
176+
getTypeScriptType =
177+
let k = getTypeScriptKeyType @a Proxy
178+
v = getTypeScriptType @b Proxy
179+
in const $ case A.toJSONKey @a of
180+
A.ToJSONKeyText{} -> "{[k in " <> k <> "]?: " <> v <> "}"
181+
A.ToJSONKeyValue{} -> getTypeScriptType @[(a, b)] Proxy
182+
getParentTypes = const $ L.nub [TSType @a Proxy, TSType @b Proxy]
183+
184+
instance (TypeScript a, TypeScript b, A.ToJSONKey a) => TypeScript (HashMap a b) where
185+
getTypeScriptType = const $ getTypeScriptType @(Map a b) Proxy
186+
getParentTypes = const $ getParentTypes @(Map a b) Proxy
181187

182188
#if MIN_VERSION_aeson(2,0,0)
183189
instance (TypeScript a) => TypeScript (A.KeyMap a) where

0 commit comments

Comments
 (0)