deriveTypeScript :: OptionsEncoding options.
-> NameName of the type for which to generate a TypeScript instance declaration.
-> Q [Dec]
Generates a TypeScript instance declaration for the given data type.
:: a typeCtrl KGHC 10.0.0.20260917 · lts/ghc-10.0.x · 8174fa4 · 2026-10-11
Moduleaeson-typescript-0.6.4.0Haskell2010
This library provides a way to generate TypeScript .d.ts files that match your existing Aeson ToJSON instances.
If you already use Aeson's Template Haskell support to derive your instances, then deriving TypeScript is as simple as
$(deriveTypeScript myAesonOptions ''MyType)
For example,
data D a = Nullary
| Unary Int
| Product String Char a
| Record { testOne :: Double
, testTwo :: Bool
, testThree :: D a
} deriving Eq
Next we derive the necessary instances.
$(deriveTypeScript (defaultOptions {fieldLabelModifier = drop 4, constructorTagModifier = map toLower}) ''D)
Now we can use the newly created instances.
>>> putStrLn $ formatTSDeclarations $ getTypeScriptDeclarations (Proxy :: Proxy (D T))
type D<T> = INullary<T> | IUnary<T> | IProduct<T> | IRecord<T>;
interface INullary<T> {
tag: "nullary";
}
interface IUnary<T> {
tag: "unary";
contents: number;
}
interface IProduct<T> {
tag: "product";
contents: [string, string, T];
}
interface IRecord<T> {
tag: "record";
One: number;
Two: boolean;
Three: D<T>;
}
It's important to make sure your JSON and TypeScript are being derived with the same options. For this reason, we include the convenience HasJSONOptions typeclass, which lets you write the options only once, like this:
instance HasJSONOptions MyType where getJSONOptions _ = (defaultOptions {fieldLabelModifier = drop 4})
$(deriveJSON (getJSONOptions (Proxy :: Proxy MyType)) ''MyType)
$(deriveTypeScript (getJSONOptions (Proxy :: Proxy MyType)) ''MyType)
Or, if you want to be even more concise and don't mind defining the instances in the same file,
myOptions = defaultOptions {fieldLabelModifier = drop 4}
$(deriveJSONAndTypeScript myOptions ''MyType)
Remembering that the Template Haskell Q monad is an ordinary monad, you can derive instances for several types at once like this:
$(mconcat <$> traverse (deriveJSONAndTypeScript myOptions) [''MyType1, ''MyType2, ''MyType3])
Once you've defined all necessary instances, you can write a main function to dump them out into a .d.ts file. For example:
main = putStrLn $ formatTSDeclarations (
(getTypeScriptDeclarations (Proxy :: Proxy MyType1)) <>
(getTypeScriptDeclarations (Proxy :: Proxy MyType2)) <>
...
)
deriveTypeScript :: OptionsEncoding options.
-> NameName of the type for which to generate a TypeScript instance declaration.
-> Q [Dec]Generates a TypeScript instance declaration for the given data type.
deriveTypeScript' :: OptionsEncoding options.
-> NameName of the type for which to generate a TypeScript instance declaration.
-> ExtraTypeScriptOptionsExtra options to control advanced features.
-> Q [Dec]Generates a TypeScript instance declaration for the given data type.
Generates a TypeScript declaration for a closed type family as a lookup type.
The typeclass that defines how a type is turned into TypeScript.
The getTypeScriptDeclarations method describes the top-level declarations that are needed for a type, while getTypeScriptType describes how references to the type should be translated. The getTypeScriptOptional method exists purely so that Maybe types can be encoded with a question mark.
Instances for common types are built-in and are usually very simple; for example,
instance TypeScript Bool where
getTypeScriptType _ = "boolean"
Most of the time you should not need to write instances by hand; in fact, the TSDeclaration
constructors are deliberately opaque. However, you may occasionally need to specify the type of something.
For example, since UTCTime is encoded to a JSON string and is not built-in to this library:
import Data.Time.Clock (UTCTime)
instance TypeScript UTCTime where
getTypeScriptType _ = "string"
If you need to write a definition for a higher-order type, it may depend on a type parameter. For example,
a Set is encoded to a JSON list of the underlying type:
instance (TypeScript a) => TypeScript (Set a) where
getTypeScriptType _ = getTypeScriptType (Proxy :: Proxy a) <> "[]";
getTypeScriptDeclarations :: Proxy a -> [TSDeclaration]Get the declaration(s) needed for this type.
getTypeScriptType :: Proxy a -> StringGet the type as a string.
getTypeScriptKeyType :: Proxy a -> StringgetTypeScriptOptional :: Proxy a -> BoolGet a flag representing whether this type is optional.
getParentTypes :: Proxy a -> [TSType]Get the types that this type depends on. This is useful for generating transitive closures of necessary types.
isGenericVariable :: Proxy a -> BoolSpecial flag to indicate whether this type corresponds to a template variable.
TypeScript ValueDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript TDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T1Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T10Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T2Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T3Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T4Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T5Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T6Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T7Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T8Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T9Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript VoidDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript IntegerDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript NaturalDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Int16Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Int32Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Int64Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript BoolDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript CharDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript DoubleDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript FloatDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript IntDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript WordDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Word16Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Word32Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Word64Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript Word8Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript TextDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript TextDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript ()Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript [Char]Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript (KeyMap a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript (Set a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript (NonEmpty a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript (Identity a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript (Maybe a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript (HashSet a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanTypeScript a => TypeScript [a]Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(TypeScript a, TypeScript b) => TypeScript (Either a b)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(TypeScript a, TypeScript b) => TypeScript (a, b)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(TypeScript a, TypeScript b, ToJSONKey a) => TypeScript (Map a b)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(TypeScript a, TypeScript b, ToJSONKey a) => TypeScript (HashMap a b)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(TypeScript a, TypeScript b, TypeScript c) => TypeScript (a, b, c)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(Typeable k, Typeable b, TypeScript a) => TypeScript (Const a b)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(TypeScript a, TypeScript b, TypeScript c, TypeScript d) => TypeScript (a, b, c, d)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(Typeable k, Typeable f, Typeable g, Typeable a, TypeScript (f a), TypeScript (g a)) => TypeScript (Product f g a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphan(Typeable k, Typeable k1, Typeable f, Typeable g, Typeable a, TypeScript (f (g a)), TypeScript a) => TypeScript (Compose f g a)Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Instances · orphanAn existential wrapper for any TypeScript instance.
forall k a. (Typeable a, TypeScript a) => TSTypeEq TSDeclarationDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesOrd TSDeclarationDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesShow TSDeclarationDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesSame as formatTSDeclarations', but uses default formatting options.
Format a list of TypeScript declarations into a string, suitable for putting directly into a .d.ts file.
Format a single TypeScript declaration. This version accepts a FormattingOptions object in case you want more control over the output.
FormattingOptionsnumIndentSpaces :: IntHow many spaces to indent TypeScript blocks
interfaceNameModifier :: String -> StringFunction applied to generated interface names
typeNameModifier :: String -> StringFunction applied to generated type names
exportMode :: ExportModeWhether to include the export keyword in declarations
typeAlternativesFormat :: SumTypeFormatHow to format the declaration of the alternatives when multiple constructors exist
The defaultNameFormatter in the FormattingOptions checks to see if the name is a legal TypeScript name. If it is not, then it throws a runtime error.
TODO: docstrings here
Eq SumTypeFormatDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesShow SumTypeFormatDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesExportEachPrefix every declaration with the "export" keyword (suitable for putting in a TypeScripe module)
ExportNoneNo exporting (suitable for putting in a .d.ts file)
Type variable gathering
Convenience typeclass class you can use to "attach" a set of Aeson encoding options to a type.
getJSONOptions :: Proxy a -> OptionsConvenience function to generate ToJSON, FromJSON, and TypeScript instances simultaneously, so the instances are guaranteed to be in sync.
This function is given mainly as an illustration. If you want some other permutation of instances, such as ToJSON and TypeScript only, just take a look at the source and write your own version.
deriveJSONAndTypeScript' :: OptionsEncoding options.
-> NameName of the type for which to generate ToJSON, FromJSON, and TypeScript instance declarations.
-> ExtraTypeScriptOptionsExtra options to control advanced features.
-> Q [Dec]TypeScript TDefined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T1Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T2Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T3Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T4Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T5Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T6Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T7Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T8Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T9Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.TypesTypeScript T10Defined in aeson-typescript-0.6.4.0 · Data.Aeson.TypeScript.Types