aeson-typescript-0.1.0.3: Generate TypeScript definition files from your ADTs

Copyright(c) 2018 Tom McLaughlin
LicenseBSD3
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Data.Aeson.TypeScript.TH

Contents

Description

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)

type D<T> = "nullary" | IUnary<T> | IProduct<T> | IRecord<T>;

type IUnary<T> = number;

type IProduct<T> = [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)

Synopsis

Documentation

deriveTypeScript Source #

Arguments

:: Options

Encoding options.

-> Name

Name of the type for which to generate a TypeScript instance declaration.

-> Q [Dec] 

Generates a TypeScript instance declaration for the given data type or data family instance constructor.

The main typeclass

class TypeScript a where Source #

The typeclass that defines how a type is turned into TypeScript.

Minimal complete definition

getTypeScriptType

Methods

getTypeScriptDeclarations :: Proxy a -> [TSDeclaration] Source #

Get the declaration(s) needed for this type.

getTypeScriptType :: Proxy a -> String Source #

Get the type as a string.

getTypeScriptOptional :: Proxy a -> Bool Source #

Get a flag representing whether this type is optional.

Formatting declarations

formatTSDeclarations :: [TSDeclaration] -> String Source #

Same as formatTSDeclarations', but uses default formatting options.

formatTSDeclaration :: FormattingOptions -> TSDeclaration -> String Source #

Format a single TypeScript declaration. This version accepts a FormattingOptions object in case you want more control over the output.

data FormattingOptions Source #

Constructors

FormattingOptions 

Fields

Convenience tools

class HasJSONOptions a where Source #

Convenience typeclass class you can use to "attach" a set of Aeson encoding options to a type.

Minimal complete definition

getJSONOptions